Dividing text in a textBox

  • Thread starter Thread starter Tomomichi Amano
  • Start date Start date
T

Tomomichi Amano

Hello
I want to divide the text in "textBox11" to ten separate textBoxes1 to 10,
separated with a divider text "====".
So, if the text in textBox11 is "Apple====Bat====Candy====Dime..." the text
in textBox1 is "Apple", textBox2 is "Bat", textBox3 is "Candy, textBox4 is
"Dime" and so on.

Thanks in advance.
 
Tomomichi Amano said:
I want to divide the text in "textBox11" to ten
separate textBoxes1 to 10, separated with
a divider text "====".
So, if the text in textBox11 is "Apple====Bat
====Candy====Dime..." the text in textBox1
is "Apple", textBox2 is "Bat", textBox3 is "Candy,
textBox4 is "Dime" and so on.

Here is one way to split up the string.

Doing things with TextBoxes is up to you. (You'll probably find it easier if
you have an array of TextBoxes, rather than individual ones with numbered
names.)

string strAll = "Apple====Bat====Candy====Dime";

ArrayList words = new ArrayList();

while (strAll != "" && strAll != "====")
{
int iDelimPos = strAll.IndexOf("====");

if (iDelimPos == 0)
{
// Found a leading delimiter
strAll = strAll.Substring("====".Length);
}
else if (iDelimPos > -1)
{
// Found a word followed by '===='
words.Add(strAll.Substring(0, iDelimPos));
strAll = strAll.Substring(strAll.IndexOf("===="));
}
else
{
// Found the last word
words.Add(strAll);
strAll = "";
}
}

for (int iWord = 0; iWord < words.Count; iWord++)
{
// do something with words[iWord]
}

P.
 
You can use a regex, but I don't use them enouph to ever remember the syntax
without going backinto the doco. However this is pretty easy. If you know
you will always split on "====" (and not "==" for example) then just
preparse the string and replace "====" with a comma "," for example. Now
you should have "Apple,Bat,Candy". Now just do a String.Split into a
string[]:

string s = "Bob====Paul====Bill";
s = s.Replace("====", ",");
string[] sa = s.Split(',');
foreach (string sepString in sa)
{
Console.WriteLine(sepString);
}
 
Tomomichi, That's a perfect use case for the String.Split() method. You can
split on the ===='s and then take the resulting array of strings and assign
them to the appropriate text boxes.
 
Greg Ewing said:
That's a perfect use case for the String.Split()
method. You can split on the ===='s

String.Split can only split on characters, not substrings, so it's not quite
that simple.

P.
 
True, if you need to do multiple characters than Regex.Split() is where you
should look. You can define any regex expression to split upon.
 
string.Replace(@"====", "\0").Split(new char[] {'\0'}) will acheive the
split into the array.
 
Back
Top