|
-
Oct 24th, 2005, 06:24 PM
#1
Thread Starter
Frenzied Member
String manipulation help needed...
I have this string:
"hello\r\nworld\r\n"
I want to parse it so that I get "hello" and "world" only. I tried using replace() to replace the \r\n with a single character such as a comma. But it doesn't work. Any ideas?
-
Oct 24th, 2005, 09:01 PM
#2
Lively Member
Re: String manipulation help needed...
 Originally Posted by benmartin101
I have this string:
"hello\r\nworld\r\n"
I want to parse it so that I get "hello" and "world" only. I tried using replace() to replace the \r\n with a single character such as a comma. But it doesn't work. Any ideas?
Unless you have @ right before the quotes it should parse the \n to carriage return and the \r should also parse to a carriage return.
strTestVariable1 = @"hello\r\nworld\r\n";
strTestVariable2 = "hello\r\nworld\r\n"
strTestVariable1 will print out:
strTestVariable2 will print out:
But if you want to attempt to use the replace function, you may want to use it on strTestVariable1. I think that will yield better results.
-
Oct 24th, 2005, 10:55 PM
#3
Re: String manipulation help needed...
-
Oct 25th, 2005, 05:26 AM
#4
Lively Member
Re: String manipulation help needed...
 Originally Posted by jmcilhinney
What code are you using?
This post is in the C# section... My advice was for C#. I don't know how to do that in VB.
Last edited by deranged; Oct 25th, 2005 at 05:37 AM.
-
Oct 25th, 2005, 08:57 PM
#5
Lively Member
Re: String manipulation help needed...
Code:
char split[] = { '\r', '\n' };
string s = "Hello\r\nWorld\r\n";
string splitstring[] = s.Split(split);
\\ splitstring[0] = "Hello"
\\ splitstring[1] = "World"
-
Oct 25th, 2005, 09:06 PM
#6
Re: String manipulation help needed...
That's not going to work properly because it will split on every occurrence of any character, so you end up with this: {"Hello", "", "World", ""}.
You can use the Regex.Split method to split on a substring, but you'd still end up with an extra empty string at the end because of the "\r\n" at the end. I'm not sure what your general case is butm for that specific case I'd recommend this:
Code:
string myString = "Hello\r\nWorld\r\n";
string[] mySubstrings = System.Text.RegularExpressions.Regex.Split(myString.TrimEnd('\r', '\n'), Environment.NewLine, null);
-
Oct 26th, 2005, 02:49 PM
#7
Thread Starter
Frenzied Member
Re: String manipulation help needed...
thanks for all the suggestions.
-
Oct 26th, 2005, 03:04 PM
#8
Lively Member
Re: String manipulation help needed...
That's not going to work properly because it will split on every occurrence of any character, so you end up with this: {"Hello", "", "World", ""}.
I see... Thanks!
-
Oct 30th, 2005, 01:12 AM
#9
Frenzied Member
Re: String manipulation help needed...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|