By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,838 Members | 2,222 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,838 IT Pros & Developers. It's quick & easy.

char[] for string split function

P: n/a
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 !
Nov 15 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
andrewcw <an******@acw.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
Did you new chSplit in the second example? You still need to allocate the
storage space for it.

"andrewcw" <an******@acw.com> wrote in message
news:03****************************@phx.gbl...
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 !

Nov 15 '05 #3

P: n/a
Did you initizialize the char array first?

char[] chSplit = new char[1];

Hope this helps,
Jacob

"andrewcw" <an******@acw.com> wrote in message
news:03****************************@phx.gbl...
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 !

Nov 15 '05 #4

P: n/a
andrewcw wrote:
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
Nov 15 '05 #5

P: n/a
mikeb <ma************@mailnull.com> wrote:
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 Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6

P: n/a
Jon Skeet [C# MVP] wrote:
mikeb <ma************@mailnull.com> wrote:
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.
--
mikeb
Nov 15 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.