Substring problem

Last post 07-07-2009 8:48 AM by ranigoal. 3 replies.

Sort Posts:

  • Substring problem

    07-06-2009, 7:07 PM
    • Member
      62 point Member
    • ranigoal
    • Member since 06-15-2006, 9:34 AM
    • Posts 295

     

    I am reading strings such as

     

    1)hhh-130.01-this is a test-this is a test 2

    2)hhh-130.1-this is a test

    3)hhh-130. -this is a test-this is a test 5

    I want only the number between the first "-" and the second "-"

    I did something like this

    string val = txtinput.Text.Substring(txtinput.Text..Text.IndexOf("-") + 1, txtinput.Text..Text.IndexOf("-"))

    but the problem I get for number 1 130.0 and i should get 130.01

    i can't do txtinput.Text..Text.IndexOf("-") + 1 for the second part because somtime I only need 130. like number 3

     

    Thanks

     

    130

  • Re: Substring problem

    07-06-2009, 8:55 PM
    Answer
    • All-Star
      90,561 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,389
    • Moderator
      TrustedFriends-MVPs

     Like this:

    Dim Data As [String] = "hhh-130.01-this is a test-this is a test 2"
    Dim Parts As [String]() = Data.Split("-"C)
    
    Dim Answer As [String] = Parts(1)
    


     (converted from C# code)

    String Data = "hhh-130.01-this is a test-this is a test 2";
    String[] Parts = Data.Split('-');
    
    String Answer = Parts[1];
    


     

    Steve Wellens

    My blog
  • Re: Substring problem

    07-06-2009, 9:15 PM
    • Star
      12,555 point Star
    • malcolms
    • Member since 06-12-2008, 12:38 AM
    • Melbourne, Australia
    • Posts 2,061

    This is perfect for LINQ.  This query returns the first and second only.

    string yourString = "hhh-130.01-this is a test-this is a test 2";
    var data = yourString.Split('-').Take(2).Skip(0).ToList();


    Sincerely,
    Malcolm Sheridan

    Microsoft Certified Solution Developer
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as
    Answer" if a marked post does not actually answer your question.
  • Re: Substring problem

    07-07-2009, 8:48 AM
    • Member
      62 point Member
    • ranigoal
    • Member since 06-15-2006, 9:34 AM
    • Posts 295

     Thank you SGWellens

Page 1 of 1 (4 items)
Microsoft Communities