Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using Split 1

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
I am using Split to split get a number of items from a multi line text box. But I need to be able to get the number of elements within the text box (e.g. if the text box has 2,4,8 in it, I want to be able to count that this text box has 3 elements entered into it). My code is below :

for (int p=0; p < dgNational.Items.Count; p++)
{
strQuantity = ((TextBox)dgNational.Items[p].FindControl("txtAccount")).Text +"\r\n";

string strDelimiter = "/r/n";
char [] Delimiter = strDelimiter.ToCharArray();

string [] strSplit = null;

for (int x=0; x <= 100; x++)
{
strSplit = strQuantity.Split(Delimiter, x);
}


if (strSplit.Length != dgNational.Items.Count)
{
lblErrorNational.Text = "Invalid number of accounts entered";
return;
}
}


Any help would be much appreciated.


Cheers,

lfc77
 
lfc77,
If I understood you correctly, you're trying to get the number of lines in the text box is one thing, and the number of comma-delimited items within each line. If that's the case, you could do this:
Code:
[COLOR=blue]string[/color] s = textBox1.Text;
[COLOR=blue]char[/color][] delimiter = "\n".ToCharArray();

[COLOR=green]// Get the lines in the text box[/color]	
[COLOR=blue]string[/color][] s2 = s.Trim().Split(delimiter);

[COLOR=green]// Display the number of lines[/color]
MessageBox.Show(s2.Length + " lines.");

[COLOR=green]//get the number of elements in each line[/color]
[COLOR=blue]int[/color] chars;
[COLOR=blue]for[/color] ([COLOR=blue]int[/color] i = 0; i < s2.Length; i++)
{
  [COLOR=green]// Get the number of items in this line[/color]
   chars = s2[i].Trim().Split(',').Length;
}
I'm not sure why, but when I used "\r\n".ToCharArray() or Environment.NewLine.ToCharArray(), split was returning the wrong number of lines (more lines than there where) in the text box. Thus, I used "\n".ToCharArray(). This may not be the most elegant solution but it works as expected. The line string[] s2 = s.Trim().Split(delimiter) gets you the number of lines in the text box, while chars = s2.Trim().Split(',').Length gets you the number of (comma-delimited) items in each a line of the text box.

Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Thanks JC!

I am now trying to get at the each element within the textbox so that I can a) check that there are no lines where nothing has been entered and b) do a database update for each item in the textbox. This is my code, but I don't really know why it is going wrong :

for (int p=0; p < dgNational.Items.Count; p++)
{
strSIPAccount = ((TextBox)dgNational.Items[p].FindControl("txtSIPAccount")).Text +"\r\n";
strQuantity = ((TextBox)dgNational.Items[p].FindControl("txtQuantity")).Text;

if (strQuantity != "")
{
//for correct number of lines use \n rather than \r\n
char[] delimiter = "\n".ToCharArray();
string [] strSplit = null;

//get the lines in the text box
strSplit = strSIPAccount.Trim().Split(delimiter);


if (strSplit.Length.ToString() != strQuantity)
{
lblErrorNational.Text = "Invalid number of SIP accounts entered";
return;
}

//check for empty rows
string strDelimiterEmpty = "/r/n";
char [] DelimiterEmpty = strDelimiterEmpty.ToCharArray();

string [] strSplitEmpty = null;

for (int x=0; x <= 100; x++)
{
strSplitEmpty = strSIPAccount.Split(DelimiterEmpty, x);
}

foreach (string s in strSplitEmpty)
{
if (s == "")
{
lblErrorNational.Text = "Invalid entry of SIP accounts";
return;
}
}
}
}


Thanks,

lfc77
 
lfc77,
Let's take a look at your code; the lines in bold require your attention.
Code:
for (int p=0; p < dgNational.Items.Count;p++)
{
  strSIPAccount = [...];
  strQuantity = [...];                

  [COLOR=green]// You should use Trim() when you test
  // for empty strings[/color]
  [b]if (strQuantity.Trim().Length > 0)[/b]
  {
    char[] delimiter = "\n".ToCharArray();
    string [] strSplit = null;

    [COLOR=green]//Get the lines in the text box[/color]    
    strSplit = ...Split(delimiter);
   
    if (... != strQuantity)
    {
      lblErrorNational.Text = "Invalid..."
      return;
    }
    
    [COLOR=green]//Check for empty rows[/color]
    string strDelimiterEmpty = "/r/n";
    char[] delEmpty = [...];
    string [] strSplitEmpty = null;

    [b]for (int x=0; x <= 100; x++)
    {
      strSplitEmpty = ...(delEmpty, x);
    }

    foreach (string s in strSplitEmpty)
    {
      if (s == "")
      {
        lblErrorNational.Text = "Invalid...";
        return;
      }
    }[/b]
  }
}
The first line in bold shows that I changed what you had before, if (strQuantity != ""), because white spaces such as "\r\n" might make the statement true when it really should be false.

The next code in bold is a bit strange. I didn't change it because I need to know a couple of this. First, in the first loop (the for x=0...), I don't know why you're looping 100 times. Plus, every pass of the loop replaces the strSplitEmpty variable, thus, at the end of the loop, strSplitEmpty will have the value returned by the last pass, which is strSplitEmpty = strSIPAccount.Split(DelimiterEmpty, 99). In other words, the first 99 passes of the loop (x=0 to 98) were useless.

If what you're trying to do is process 100 lines, then you should include the second loop inside the first one, like this:
Code:
[COLOR=blue]for[/color] ([COLOR=blue]int[/color] x=0; x <= 100; x++)
{
  strSplitEmpty = ...(delEmpty, x);
  
  [COLOR=green]// Process the current line here[/color]
  [COLOR=blue]foreach[/color] ([COLOR=blue]string[/color] s [COLOR=blue]in[/color] strSplitEmpty)
  {
    [b][COLOR=blue]if[/color] (s.Trim().Length == 0)[/b]
    {
      lblErrorNational.Text = "Invalid...";
      [COLOR=blue]return[/color];
    }
  }
}
That may lead you in the right path. But, if it doesn't, let me know exactly what you're trying to do. When something doesn't work, tell me exactly why it's not working. In other words, tell me if you get any error messages, get the wrong results, etc...

Hope this helps!

JC

Friends are angels who lift us to our feet when our wings have trouble remembering how to fly...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top