split the string return by the sqlserver 2005

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pankajprakash
    New Member
    • Feb 2007
    • 70

    #1

    split the string return by the sqlserver 2005

    hi,

    i have a string which is return by the sqlserver 2005, which contains a "-" (separator), now i need to split this string, but i occurs error.

    string[] a ; // a = "Code - N/A" this is returned by the database
    string ret = "";
    a = ret.Split(Conve rt.ToChar('-'));
    ret = a[1]; //error

    will you please tell me the solution.
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Originally posted by pankajprakash
    hi,

    i have a string which is return by the sqlserver 2005, which contains a "-" (separator), now i need to split this string, but i occurs error.

    string[] a ; // a = "Code - N/A" this is returned by the database
    string ret = "";
    a = ret.Split(Conve rt.ToChar('-'));
    ret = a[1]; //error

    will you please tell me the solution.
    Hi,
    what i believe that the string ret is returned by the database rather than strin[] a;

    Ok, a[1] refers to the second sting.

    so if the string ret does not contain any '-' then a[1] will not be created and that is why it will give the index out of range exception whcih you have not mentioned about in your post.(you should tell us what sort of error, rather than us trying to figure out what the error is)

    cheers

    Comment

    • GayathriP
      New Member
      • Oct 2007
      • 17

      #3
      The string ret do not have any value and hence when split will return a zero length array. So while referring a[1] returning the error.

      Comment

      • pankajprakash
        New Member
        • Feb 2007
        • 70

        #4
        actually ret contains the string which is returned by the database so the value is
        ret is "Code - N/A"

        not ret contains the "-" value but as soon as i split this string by the "-"

        string ret = getString(); //getString() returns the string from database
        a = ret.Split(Conve rt.ToChar('-'));
        ret = a[1]; //error

        it does not spit the string by the "-" that's why error is occurring. I don't understand string contains "-" char but it does not split the string.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          You appear to be mixing up your variables, assigning the wrong one the [] array indicator and such.

          Examine the following:
          Code:
          string a="Code - N/A";
          string[] splitvals= a.Split("-".ToCharArray());
          //now then splitvals is the following:
          //splitvals[0]="Code "
          //splitvals[1]=" N/A"
          It may be benificial to put a breakpoint on the .Split() line so you can see the value actually returned by the database, and make sure it contains a "-"

          Originally posted by pankajprakash
          actually ret contains the string which is returned by the database so the value is
          ret is "Code - N/A"

          not ret contains the "-" value but as soon as i split this string by the "-"

          string ret = getString(); //getString() returns the string from database
          a = ret.Split(Conve rt.ToChar('-'));
          ret = a[1]; //error

          it does not spit the string by the "-" that's why error is occurring. I don't understand string contains "-" char but it does not split the string.

          Comment

          Working...