splitting a string by a delimiter and traversing it

  • Thread starter Mad Scientist Jr
  • Start date
M

Mad Scientist Jr

I have been doing this kind of thing in VB and VB.net for a long time,
but am having problems in C#:

First I want to split up a string by the backslash character, but this
line:
arrSplit = strTemp.Split( (@"\") );

results in the error:

The best overloaded method match for 'string.Split(params char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharArray() );


which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLowerBound( 0 ); iLoop <=
arrSplit.GetUpperBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpper;

The above line gives the error:
Cannot apply indexing with [] to an expression of type 'System.Array'

Can anyone tell me what I'm doing wrong and how to split the string &
loop through the parts?

Thanks
 
J

James Curran

arrSplit = strTemp.Split (new char[] { '\' });

You could do that explicitly, or, since this is a "params" paramters,
you can let the compiler build the array for you:

arrSplit = strTemp.Split ( @'\' ); // note: single-quotes, to
indicate a single character, not a string.


--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Michael C# said:
arrSplit = strTemp.Split (new char[] { '\' });

Mad Scientist Jr said:
I have been doing this kind of thing in VB and VB.net for a long time,
but am having problems in C#:

First I want to split up a string by the backslash character, but this
line:
arrSplit = strTemp.Split( (@"\") );

results in the error:

The best overloaded method match for 'string.Split(params char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharArray() );


which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLowerBound( 0 ); iLoop <=
arrSplit.GetUpperBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpper;

The above line gives the error:
Cannot apply indexing with [] to an expression of type 'System.Array'

Can anyone tell me what I'm doing wrong and how to split the string &
loop through the parts?

Thanks
 
M

Michael C#

Good catch. I did a "quickie convert" of some code, since I usually declare
my separator array beforehand - so I don't have to keep re-creating it every
time I call Split. Thanks

James Curran said:
arrSplit = strTemp.Split (new char[] { '\' });

You could do that explicitly, or, since this is a "params" paramters,
you can let the compiler build the array for you:

arrSplit = strTemp.Split ( @'\' ); // note: single-quotes, to
indicate a single character, not a string.


--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Michael C# said:
arrSplit = strTemp.Split (new char[] { '\' });

Mad Scientist Jr said:
I have been doing this kind of thing in VB and VB.net for a long time,
but am having problems in C#:

First I want to split up a string by the backslash character, but this
line:
arrSplit = strTemp.Split( (@"\") );

results in the error:

The best overloaded method match for 'string.Split(params char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharArray() );


which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLowerBound( 0 ); iLoop <=
arrSplit.GetUpperBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpper;

The above line gives the error:
Cannot apply indexing with [] to an expression of type 'System.Array'

Can anyone tell me what I'm doing wrong and how to split the string &
loop through the parts?

Thanks
 
J

James Curran

Unfortunately, I should have tried that before I posted it..

@ doesn't work with single chars, only with strings. You have to write
it as

arrSplit = strTemp.Split ( '\\' );

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

James Curran said:
arrSplit = strTemp.Split (new char[] { '\' });

You could do that explicitly, or, since this is a "params" paramters,
you can let the compiler build the array for you:

arrSplit = strTemp.Split ( @'\' ); // note: single-quotes, to
indicate a single character, not a string.


--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Michael C# said:
arrSplit = strTemp.Split (new char[] { '\' });

Mad Scientist Jr said:
I have been doing this kind of thing in VB and VB.net for a long time,
but am having problems in C#:

First I want to split up a string by the backslash character, but this
line:
arrSplit = strTemp.Split( (@"\") );

results in the error:

The best overloaded method match for 'string.Split(params char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharArray() );


which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLowerBound( 0 ); iLoop <=
arrSplit.GetUpperBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpper;

The above line gives the error:
Cannot apply indexing with [] to an expression of type 'System.Array'

Can anyone tell me what I'm doing wrong and how to split the string &
loop through the parts?

Thanks
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

Also be careful using split. There is a problem with split in @ Framework
1.1
string str="a..b";
string[] sArray= str.Split('.');

The returned string array contains three items.

But this problem is fixed in @ Framework 2.0.

Keep this in mind !!!
--

Thanks,
Yunus Emre ALPÖZEN
BSc, MCAD.NET

James Curran said:
Unfortunately, I should have tried that before I posted it..

@ doesn't work with single chars, only with strings. You have to write
it as

arrSplit = strTemp.Split ( '\\' );

--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

James Curran said:
arrSplit = strTemp.Split (new char[] { '\' });

You could do that explicitly, or, since this is a "params" paramters,
you can let the compiler build the array for you:

arrSplit = strTemp.Split ( @'\' ); // note: single-quotes, to
indicate a single character, not a string.


--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Michael C# said:
arrSplit = strTemp.Split (new char[] { '\' });

I have been doing this kind of thing in VB and VB.net for a long time,
but am having problems in C#:

First I want to split up a string by the backslash character, but
this
line:
arrSplit = strTemp.Split( (@"\") );

results in the error:

The best overloaded method match for 'string.Split(params char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharArray() );


which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLowerBound( 0 ); iLoop <=
arrSplit.GetUpperBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpper;

The above line gives the error:
Cannot apply indexing with [] to an expression of type
'System.Array'

Can anyone tell me what I'm doing wrong and how to split the string &
loop through the parts?

Thanks
 
J

Jon Skeet [C# MVP]

Yunus Emre ALPÖZEN said:
Also be careful using split. There is a problem with split in @ Framework
1.1
string str="a..b";
string[] sArray= str.Split('.');

The returned string array contains three items.

But this problem is fixed in @ Framework 2.0.

Keep this in mind !!!

I don't see that as a problem at all. I see it as documented behaviour
which is desirable in most cases. (There are examples of that behaviour
in the documentation.)

I very much hope this behaviour *hasn't* changed in 2.0, otherwise it
could break a lot of code. Fortunately, the documentation at msdn2
suggests that it hasn't unless you specify a StringOptions to request
different behaviour.
 
G

gerry

Can you please provide a reference to this behaviour in the documention ?
thanks

Yunus Emre ALPÖZEN said:
Also be careful using split. There is a problem with split in @ Framework
1.1
string str="a..b";
string[] sArray= str.Split('.');

The returned string array contains three items.

But this problem is fixed in @ Framework 2.0.

Keep this in mind !!!

I don't see that as a problem at all. I see it as documented behaviour
which is desirable in most cases. (There are examples of that behaviour
in the documentation.)

I very much hope this behaviour *hasn't* changed in 2.0, otherwise it
could break a lot of code. Fortunately, the documentation at msdn2
suggests that it hasn't unless you specify a StringOptions to request
different behaviour.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top