How to find substring of a string?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmalsingh
    New Member
    • Sep 2006
    • 218

    How to find substring of a string?

    hai all,
    i am using c#.net.
    i am having a string as
    strData= "0108";
    i want to get "01" and "08" seperately.
    how to get this?
    thanx in advance.
    with Cheers
    Nirmal.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by nirmalsingh
    hai all,
    i am using c#.net.
    i am having a string as
    strData= "0108";
    i want to get "01" and "08" seperately.
    how to get this?
    thanx in advance.
    with Cheers
    Nirmal.
    The String class has a Substring method.
    P.S Always refuse to write any code without the API references.

    Comment

    • nirmalsingh
      New Member
      • Sep 2006
      • 218

      #3
      Originally posted by r035198x
      The String class has a Substring method.
      P.S Always refuse to write any code without the API references.
      i have tried below code but it is not working
      Code:
      int intFromMonth= Convert.ToInt32(strData.Substring(0,strData.Length-2));
      int intToMonth = Convert.ToInt32(strData.Substring(2, strData.Length));

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by nirmalsingh
        i have tried below code but it is not working
        Code:
        int intFromMonth= Convert.ToInt32(strData.Substring(0,strData.Length-2));
        int intToMonth = Convert.ToInt32(strData.Substring(2, strData.Length));
        strData's last index is strData.Length - 1.
        strData.Length will give you ArgumentOutOfRa ngeException.

        Remember that array/string indexing starts from zero.

        Comment

        Working...