954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

invalid string split char

Hi,

string[] SplitEquationText = EquationBox.Text.Split('');

in that line of code, it will not let me split nothing? am i allowed to split nothing?
thank you.

choover12
Newbie Poster
22 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

Exactly what would it be splitting the string on? No, you can't split a string using no character.

I would really like to see an example of what you thought would happen.

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

I will show you a sime example of to split array. Here you go:
//this is a button1_Click method, or could be even anything else:

string value = textBox1.Text;
if(value.Lenght > 0)
{
     string[] array = value.Split(' '); //split by whitespace
     string rows = null;
     int i = 0;
     while ( i < array.Lenght)
     {
           rows += array[i] + Environment.NewLine;
           i++;
     }
     MesssageBox.Show("The mesage:\n" + + rows + "was split into " + array.Lenght.ToString() + " parts.");
}

Hope this helps you to understand the "split" method, using whitespace as splitter.
Mitja
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

i know how to spit. i just want something like "Hello World" to look like: "H e l l o W o r l d" so i could remove something from it. i could do if (this.Contains("this")) but it would not work.

choover12
Newbie Poster
22 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

I dont really understand you what you want now. What is exactly you are traing to achive?
Would you like from string "Hello World", change it to "H e l l o W o r l d"?

And what concerns your 1st question in your 1st post, YES, you are allowed to split nothing. If you have in mind example "thisIsTestString" - here is no white spaces, so code wont split - but there will still be NO error. There will be 1 array (same as 1 string), in a word - wortheless to have an array, but as said, no error - so you are allowed to split.

Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

For example how "Hello World" change to "H e l l o W o r l d":

string value = "Hello World";
char[] letters = value.ToCharArray();
string newValue = null;
foreach(char letter in letters)
{
   newValue += letter + " ";
}
MessageBox.Show(newValue);
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
 

Use string.Join method.

string text = "Hello World";
 string s=string.Join<char>(" ", text);
 Console.WriteLine(s);
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: