char[] for string split function

  • Thread starter Thread starter andrewcw
  • Start date Start date
A

andrewcw

The split function takes as a parameter what I understand
as array of Char.

I can define the delimiter like this:
string [] innerText = new string[2];
char[] chSplit={'='};
innerText=tempText.Split(chSplit);

But if I try this ( to make the delimiter a variable ):

char foo =Convert.ToChar(delim.Substring(0,1));
chSplit[0]=foo;

I get a compile error saying "use of unassigned local
variable chSplit"

Please explain to me what is happening and what should be
done. Thanks !
 
andrewcw said:
The split function takes as a parameter what I understand
as array of Char.

I can define the delimiter like this:
string [] innerText = new string[2];

This "= new string[2]" has no use, because you're about to reassign the
value of innerText anyway, two lines later.
char[] chSplit={'='};
innerText=tempText.Split(chSplit);

But if I try this ( to make the delimiter a variable ):

char foo =Convert.ToChar(delim.Substring(0,1));
chSplit[0]=foo;

I get a compile error saying "use of unassigned local
variable chSplit"

Please explain to me what is happening and what should be
done. Thanks !

Presumably you've done that by doing:

char[] chSplit;
// This line is more easily written as char foo = delim[0];
char foo = Convert.ToChar(delim.Substring(0,1));
chSplit[0]=foo;

The point is that chSplit itself hasn't been assigned a value, so you
can't access the value to put foo's value in the 0th element.

Fortunately, there's something else you've missed which makes life a
lot easier: the method signature is actually
String.Split(params char[])
so you can just do:

string[] innerText = tempText.Split('=');

for instance - the array is created automatically for you.
 
Did you new chSplit in the second example? You still need to allocate the
storage space for it.
 
Did you initizialize the char array first?

char[] chSplit = new char[1];

Hope this helps,
Jacob
 
andrewcw said:
The split function takes as a parameter what I understand
as array of Char.

I can define the delimiter like this:
string [] innerText = new string[2];
char[] chSplit={'='};
innerText=tempText.Split(chSplit);

But if I try this ( to make the delimiter a variable ):

char foo =Convert.ToChar(delim.Substring(0,1));
chSplit[0]=foo;

I get a compile error saying "use of unassigned local
variable chSplit"

Please explain to me what is happening and what should be
done. Thanks !

It works fine for me:

string tempText = "one = three";
string delim = "=,-/";

char [] chSplit = new char [1];
char foo =Convert.ToChar(delim.Substring(0,1));
chSplit[0]=foo;

string [] innerText = tempText.Split( chSplit);

A better alternative is:

string tempText = "one = three";
string delim = "=,-/";

string [] innerText = tempText.Split( delim.ToCharArray(0,1));
 
mikeb said:
A better alternative is:

string tempText = "one = three";
string delim = "=,-/";

string [] innerText = tempText.Split( delim.ToCharArray(0,1));

An even better alternative is:

string tempText = "one = three";
string delim = "=,-/";

string[] innerText = tempText.Split(delim[0]);

It basically does the same thing, but in a more readable way (IMO).
 
Jon said:
mikeb said:
A better alternative is:

string tempText = "one = three";
string delim = "=,-/";

string [] innerText = tempText.Split( delim.ToCharArray(0,1));


An even better alternative is:

string tempText = "one = three";
string delim = "=,-/";

string[] innerText = tempText.Split(delim[0]);

It basically does the same thing, but in a more readable way (IMO).

Well, you learn something new every day. I never caught that the
parameter to Split() was a 'params' parameter... I had always thought
that it was a plain ol' char array.
 
Back
Top