473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Simple String split

AMP
Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");

What am I doing wrong?
Thanks
Mike
Mar 11 '08 #1
7 9284
"AMP" <am******@gmail.comwrote:
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");
What am I doing wrong?
Depending on where you got it, the newline sequence might also be \n or
\r\n

Try studying the variable with a Watch in the debugger.

Eq.
Mar 11 '08 #2
AMP
On Mar 11, 9:23*am, "Paul E Collins" <find_my_real_addr...@CL4.org>
wrote:
"AMP" <ampel...@gmail.comwrote:
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");
What am I doing wrong?

Depending on where you got it, the newline sequence might also be \n or
\r\n

Try studying the variable with a Watch in the debugger.

Eq.
Either way it doesnt work:
Argument '1': cannot convert from 'string' to 'char[]'
Thanks
mike
Mar 11 '08 #3

"AMP" <am******@gmail.comwrote in message
news:77**********************************@u69g2000 hse.googlegroups.com...
Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");
Try:-

String[] Channel = FileName.Split('\r');

Also are you using the Visual Studio?

--
Anthony Jones - MVP ASP/ASP.NET
Mar 11 '08 #4
Anthony Jones skrev:
"AMP" <am******@gmail.comwrote in message
news:77**********************************@u69g2000 hse.googlegroups.com...
>Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");

Try:-

String[] Channel = FileName.Split('\r');
To be sure use \n instead of \r because newlines in a text file created
on a unix/linux system a text line is terminated by a single "\n", while
in DOS/Windows it is "\r\n".

String[] Channel = FileName.Split('\n');

Secondly: If the text line is from a DOS/Windows text file you still
have to remove the remaining \r or \n depending on what separator you
use in the split statement.

String arg0 = Channel[0].trim("\r\n");

and so on....

Personally I ended up in implementing this function:

public static String[] Str2Lines(String s, int optional_max_count)

--
Bjørn Brox
Mar 11 '08 #5
"Bjørn Brox" <bp****@gmail.comwrote in message
news:47********@news.broadpark.no...
Anthony Jones skrev:
"AMP" <am******@gmail.comwrote in message
news:77**********************************@u69g2000 hse.googlegroups.com...
Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");
Try:-

String[] Channel = FileName.Split('\r');
To be sure use \n instead of \r because newlines in a text file created
on a unix/linux system a text line is terminated by a single "\n", while
in DOS/Windows it is "\r\n".

String[] Channel = FileName.Split('\n');

Secondly: If the text line is from a DOS/Windows text file you still
have to remove the remaining \r or \n depending on what separator you
use in the split statement.

String arg0 = Channel[0].trim("\r\n");

and so on....

Personally I ended up in implementing this function:

public static String[] Str2Lines(String s, int optional_max_count)
It mystifies me why that isn't an override of split as standard.
--
Anthony Jones - MVP ASP/ASP.NET
Mar 11 '08 #6
String[] Channel = FileName.Split(new string[] { "\\r" },
StringSplitOptions.RemoveEmptyEntries);
"AMP" <am******@gmail.comschreef in bericht
news:77**********************************@u69g2000 hse.googlegroups.com...
Hello,
I am trying to split a string at the newline and this doesnt work:
String[] Channel = FileName.Split("\r");

What am I doing wrong?
Thanks
Mike
Mar 11 '08 #7
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP*********************@msnews.microsoft.com. ..
Anthony Jones <An*@yadayadayada.comwrote:

<snip>
It mystifies me why that isn't an override of split as standard.

To split lines?
I meant a simple overload of split(string delimiter). Thus s.split("\r\n")
would work.
>Could be handy, I guess - you can always add your own
extension method (in C# 3, anyway).

That would be nice but most of our customers have only just moved to 2.0 and
I'm still wary of using C# 3 in that environment. As yet I've not
discovered a way to ensure ASPX pages and App_Code files don't go out to
these customers with C# 3 code in them. So at the moment I'm having to
stick with C# 2. :(

Besides C# 3 is useful but not as useful as also having LINQ would be if the
sites had 3.5.
--
Anthony Jones - MVP ASP/ASP.NET
Mar 11 '08 #8

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

Similar topics

10
by: george young | last post by:
For each run of my app, I have a known set of (<100) wafer names. Names are sometimes simply integers, sometimes a short string, and sometimes a short string followed by an integer, e.g.: 5, 6,...
2
by: nix | last post by:
Hi I want to create a simple user validation script without using a database. Let's say I have a text file in my asp folder with a list of valid usernames. How can i do something like the...
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
1
by: steve smith | last post by:
Hi I have just downloaded the Borland C# Builder and the Micorsoft ..Net framework SDK v1.1 from the borland webist, and i am trying to get a simple program to run, however I keep getting errors,...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
7
by: Edwin Knoppert | last post by:
Should i use string or String ? string seems 'old-fashioned' (ansi?) c string while String is depending on the using/system stuff. They might be equal and can be intermixed? Maybe it's an ansi...
7
by: Matthias Winterland | last post by:
Hi, I have a simple question. When I read in a string like: a='1,2,3,4,5 6 7,3,4', can I get the list l= with a single split-call? Thx, Matthias
23
by: Rex | last post by:
Hi I want to write a procedure which takes in a string of names seperated by a whitespace and puts commas at each whitespace the last name however, should have "and" before it. Let me explain...
3
by: afrobeard | last post by:
The following following code fails with the failiure:- File "test.py", line 27, in __main__.sanitize_number Failed example: sanitize_number('0321-4683113') Expected: '03214683113' Got:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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,...
0
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...

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.