Parse string to integer C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    Parse string to integer C#

    I have a page set where i get data in following format

    41128°°8/20/2007 12:00:00 AM°11:00°True

    These data are stored in an arraylist name d. Then i use following code to split the data

    Code:
     ArrayList d = new ArrayList();
            d = x.GetDataList();  //x is a class ; GetDataList returns data as Arraylist
    
     //manipulating data
                string data = d[0].ToString();
                char deli = '°';
                string[] load = data.Split(deli);
    
                string shid = load[0].ToString();
                string pnum = load[1].ToString();
                string pdate = load[2].ToString();
                string ptime = load[3].ToString();
                string pfcfs = load[4].ToString();
    when i display the data string shid i get the value 41128. But when i try to use Int32.Parse to convert the value to an integer i get an error Input string was not in correct format. The code is

    Code:
    lblerr.Text = Int32.Parse(shid);
    After getting the input string error. I used .Trim() removing any form of spaces but still i get the same error. Please advice.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Have you used the debugger to examine exactly what is in the string?
    Then making sure the .Length property of the string is not longer then it should be?
    For your example "41128" should have a .Length of 5

    Comment

    • semomaniz
      Recognized Expert New Member
      • Oct 2007
      • 210

      #3
      I got the problem solved. When i used the . Length method i got the value 9 rather than 5. The i changed the delimiter from '°' to '^' and got the data as following format

      41128^^8/20/2007 12:00:00 AM^11:00^True

      All the code work perfectly. I am assuming '°' (ctrl + 248) some how added extra length to the string.

      Thank you

      Comment

      Working...