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?
Printable View
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.Quote:
Originally Posted by benmartin101
strTestVariable1 = @"hello\r\nworld\r\n";
strTestVariable2 = "hello\r\nworld\r\n"
strTestVariable1 will print out:
strTestVariable2 will print out:Code:hello\r\nworld\r\n
Code:hello
world
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.
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.Quote:
Originally Posted by jmcilhinney
Code:char split[] = { '\r', '\n' };
string s = "Hello\r\nWorld\r\n";
string splitstring[] = s.Split(split);
\\ splitstring[0] = "Hello"
\\ splitstring[1] = "World"
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);
thanks for all the suggestions.
I see... Thanks!Quote:
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", ""}.
Use regex