C# Split gives an error
I have an array of Strings
String[] theStringArray ={"A,B,C,D,E,F,G",
"H,I,J,K,L,M,N"};
I have an empty string array I am trying to set its contents to the contents of the 0 index of the above array.
String[] theNewArray;
theNewArray = theStringArray[0].Split(",");
Thi开发者_开发技巧s gives an error.What I have Done wrong?
Use simple quotes
String[] theNewArray;
theNewArray = theStringArray[0].Split(',');
String
is not implicitly convertible to char[]
, which is what Split
expects.
theNewArray = theStringArray[0].Split(',');
If you need to split by more than one character, you can use
theNewArray = theStringArray[0].Split(",.;:".ToCharArray());
In the future, it is helpful to tell what error message you're getting. :)
The Split()
method takes a char, not a string. Change your code to this (note the single quotes):
theNewArray = theStringArray[0].Split(',');
The error you're getting is:
The best overloaded method match for 'string.Split(params char[])' has some invalid arguments
This is because you're passing the Split method a string (double-quotes) rather than a character (single-quotes).
Try this instead:
theNewArray = theStringArray[0].Split(',');
hey man use theString.split(",");
like this
http://www.dotnetperls.com/string-split
You must use single quotes to specify you mean a char[] and not a string, even if the string is one char long.
It is also worth noting that Join requires double quotes. Quite unintuitive don't you think?!
var joinString = string.Join("|", join);
var string[] split = joinString.Split('|');
精彩评论