473,657 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

splitting a string by a delimiter and traversing it

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(p arams char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharAr ray() );
which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLow erBound( 0 ); iLoop <=
arrSplit.GetUpp erBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpp er;

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

Nov 17 '05 #1
7 13771
arrSplit = strTemp.Split (new char[] { '\' });

"Mad Scientist Jr" <us************ *@yahoo.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
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(p arams char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharAr ray() );
which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLow erBound( 0 ); iLoop <=
arrSplit.GetUpp erBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpp er;

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

Nov 17 '05 #2
> 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#" <ho***@boutdat. com> wrote in message
news:#M******** ******@TK2MSFTN GP14.phx.gbl...
arrSplit = strTemp.Split (new char[] { '\' });

"Mad Scientist Jr" <us************ *@yahoo.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
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(p arams char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharAr ray() );
which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLow erBound( 0 ); iLoop <=
arrSplit.GetUpp erBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpp er;

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


Nov 17 '05 #3
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" <ja*********@mv ps.org> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
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#" <ho***@boutdat. com> wrote in message
news:#M******** ******@TK2MSFTN GP14.phx.gbl...
arrSplit = strTemp.Split (new char[] { '\' });

"Mad Scientist Jr" <us************ *@yahoo.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
>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(p arams char[])'
> has some invalid arguments
>
> So I tried this instead:
>
> arrSplit = strTemp.Split( (@"\").ToCharAr ray() );
>
>
> which does not error. However a few lines down, there is now a new
> error:
>
> for (iLoop = arrSplit.GetLow erBound( 0 ); iLoop <=
> arrSplit.GetUpp erBound( 0 ); iLoop++)
> {
> strTemp = arrSplit[iLoop].ToString.ToUpp er;
>
> 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
>



Nov 17 '05 #4
Unfortunately, I should have tried that before I posted it..
arrSplit = strTemp.Split ( @'\' );
@ 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" <ja*********@mv ps.org> wrote in message
news:#3******** ******@TK2MSFTN GP15.phx.gbl... 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#" <ho***@boutdat. com> wrote in message
news:#M******** ******@TK2MSFTN GP14.phx.gbl...
arrSplit = strTemp.Split (new char[] { '\' });

"Mad Scientist Jr" <us************ *@yahoo.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.com...
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(p arams char[])'
has some invalid arguments

So I tried this instead:

arrSplit = strTemp.Split( (@"\").ToCharAr ray() );
which does not error. However a few lines down, there is now a new
error:

for (iLoop = arrSplit.GetLow erBound( 0 ); iLoop <=
arrSplit.GetUpp erBound( 0 ); iLoop++)
{
strTemp = arrSplit[iLoop].ToString.ToUpp er;

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



Nov 17 '05 #5
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" <ja*********@mv ps.org> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
Unfortunately, I should have tried that before I posted it..
arrSplit = strTemp.Split ( @'\' );
@ 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" <ja*********@mv ps.org> wrote in message
news:#3******** ******@TK2MSFTN GP15.phx.gbl... > 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#" <ho***@boutdat. com> wrote in message
news:#M******** ******@TK2MSFTN GP14.phx.gbl...
> arrSplit = strTemp.Split (new char[] { '\' });
>
> "Mad Scientist Jr" <us************ *@yahoo.com> wrote in message
> news:11******** *************@g 14g2000cwa.goog legroups.com...
> >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(p arams char[])'
> > has some invalid arguments
> >
> > So I tried this instead:
> >
> > arrSplit = strTemp.Split( (@"\").ToCharAr ray() );
> >
> >
> > which does not error. However a few lines down, there is now a new
> > error:
> >
> > for (iLoop = arrSplit.GetLow erBound( 0 ); iLoop <=
> > arrSplit.GetUpp erBound( 0 ); iLoop++)
> > {
> > strTemp = arrSplit[iLoop].ToString.ToUpp er;
> >
> > 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
> >
>
>



Nov 17 '05 #6
Yunus Emre ALPÖZEN [MCAD.NET] <ye***@msakadem ik.net> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7
Can you please provide a reference to this behaviour in the documention ?
thanks

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Yunus Emre ALPÖZEN [MCAD.NET] <ye***@msakadem ik.net> wrote:
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.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2227
by: qwweeeit | last post by:
Hi all, I am writing a script to visualize (and print) the web references hidden in the html files as: '<a href="web reference"> underlined reference</a>' Optimizing my code, I found that an essential step is: splitting on a word (in this case 'href'). I am asking if there is some alternative (more pythonic...): # SplitMultichar.py
8
1671
by: Anthony Liu | last post by:
I have this simple string: mystr = 'this_NP is_VL funny_JJ' I want to split it and give me a list as 1. I tried mystr.split('_| '), but this gave me:
5
2959
by: fatted | last post by:
I'm trying to write a function which splits a string (possibly multiple times) on a particular character and returns the strings which has been split. What I have below is kind of (oh dear!) printing the results I expect, which I guess means my dynamic memory allocation is a mess. Also, I was advised previously that I should really free memory in the same place I declare it, but I'm not sure how I would go about doing this in my code...
1
1712
by: Guadala Harry | last post by:
I have a string that must be split out into a string array. The segment delimiter is like this: {flag:xxx} and the "xxx" part is unknown ahead of time (can be just about any character AND any number of characters (at least one). Here is a valid sample: string sStartWithThis = "this goes to element one {flag:x} this goes to element 2 {flag:pxedqr} this goes to element 3 {flag:pzf} this goes to element 4.
20
3693
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up the data to assign each value to its own variable. So right now I am just saving the data to a txt file and when I look in the text file all the data is there. Not sure if this matters but when I open the text file in Word pad (Rich Text) It...
1
2164
by: sadiewms | last post by:
Hello! I have a flat file that I'm trying to get into a relational Access 2003 DB. One of the fields in the flat file has a list of names separated by ";". I'd like to loop through them and create new records for each in a table relational DB. ( name1; name2; name3; name4) I'm getting stuck on splitting the string. Because there may be up to 6 names in the original field I can't use the Left(), Mid() functions. I've also tried...
1
1351
by: Gustav | last post by:
Hi! I use a regex (?<!\\?)('|\\+|:) to split a string to a String. The String i get after splitting is correctly splitted but contains all the delimiters i use to decide where the string should be splitted. Do I have to loop through the array and find every index containing a single delimiter or is there a easier way to do this.
13
1976
by: Pedro Pinto | last post by:
Hi there. I'm trying to do the following. I have a string, and i want to separate it into other halves. This is how it should be: char string = "test//test2//test3"; were // is the part were i want to separate it and store on a
2
3264
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to lines it worked quite well but srttok isnot working for multiple blank or commas. Can strtok do this kind of splitting if it cant what should i use . Unal
0
8425
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5647
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.