Results 1 to 9 of 9

Thread: String manipulation help needed...

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    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?

  2. #2
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: String manipulation help needed...

    Quote 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:
    Code:
    hello\r\nworld\r\n
    strTestVariable2 will print out:
    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.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: String manipulation help needed...

    What code are you using?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Lively Member deranged's Avatar
    Join Date
    Jun 2004
    Location
    TN
    Posts
    104

    Re: String manipulation help needed...

    Quote 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.

  5. #5
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    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"

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: String manipulation help needed...

    thanks for all the suggestions.

  8. #8
    Lively Member DanielS1324's Avatar
    Join Date
    Apr 2005
    Posts
    79

    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!

  9. #9
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256

    Re: String manipulation help needed...

    Use regex

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width