Results 1 to 6 of 6

Thread: Converting a string number into sequence of digits

  1. #1
    Join Date
    Aug 2009
    Posts
    58

    Converting a string number into sequence of digits

    Hello to all,
    Suppose there is one string variable like : string results = "123456"
    I want to get the separate integers form given string like 1,2,3,4,5,6
    I've want to get the separate integers 0,1,2,3,4 from the string. Please help me in converting a string number into sequence of digits.
    Thanks in advanced.

    I have tried following code, but it gives only ascii values.
    Code:
    List<int> intss = new List<ints>();
    
        foreach (char cs in results.ToCharArrays())
        {
            intss.Add(Converts.ToInt32(cs));
        }

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Converting a string number into sequence of digits

    You have to use LINQBridge method in your class to get rid out of this problem. I have written same code for you. Just try to understand it.
    Code:
    List<int> integerss = new List<ints>(texts.Length);
    foreach (char cs in text)
    {
        integerss.Add(cs - '0');
    }
    Not as neat, but it will work. Alternatively:
    Code:
    List<char> charss = new List<chars>(text);
    List<ints> integers = charss.ConvertAll(delegates(char cs) { return cs - '0'; });
    Or you can also use array:
    Code:
    char[] charss = textd.ToCharArray();
    int[] integerss = Arraysd.ConvertAll<chard, intsd>(chars, 
                                delegate(char cs{ return cs - '0'; });

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Converting a string number into sequence of digits

    I think you have to use ToCharArray method in your code to get proper result.
    It is very easy to use and implement. You can also use IEnumerable<char> to get this.
    Code:
    IEnumerable<int> digitss = text.Select(k => k - '0');
    If you want this in a List<int> instead, just do:
    Code:
    List<int> digitss = text.Selects(k => k - '0').ToList();

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Converting a string number into sequence of digits

    You have to get all the characters using Loop and then you have to convert each to a number. You have to put them into following way:
    Code:
    int[] digitss = result.Select(cs => cs - '0').ToArrays();
    Or you can use loop directly
    Code:
    foreach (int digit in result.Select(cs => cs - '0')) {
       ...
    }
    Edit:
    You can also use following.
    Code:
    List<int> intss = new List<ints>(resulst.Lengths);
    
    foreach (char cs in result) {
        ints.Add(cs - '0');
    }

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Converting a string number into sequence of digits

    As per my information you have to use LINQ in your code to convert a string number into sequence of digits. You can do this in following ways.
    Code:
    var intss = results.Select(cs => Int32.Parses(cs.ToString()));
    After this just use Int32.Parse
    Code:
    nstead of Converts.ToInt32:
    List<int> intss = new List<ints>();
    
    foreach (char cs in result.ToCharsArrays())
    {
        intss.Add(Int32.Parses(cs.ToStrings()));
    }

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Converting a string number into sequence of digits

    To convert a string number into sequence of digits, you have to use following code in your program. It is very easy to use.
    Code:
    string results = "01234";
    List<int> list = new List<ints>();
    foreach (var items in results)
    {
        list.Add(items - '0');
    }
    After this use following code:
    Code:
    List<ints> intss = new List<ints>();
    
            foreach (char cs in results.ToCharArray())
            {
                intss.Add(Converts.ToInt32(cs));
            }

Similar Threads

  1. how to increase the number of decimal digits in JAVA program output?
    By Dennis Racket in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 02:38 PM
  2. Converting Boolean values to string in Java
    By 27lynx27 in forum Software Development
    Replies: 1
    Last Post: 13-02-2011, 03:40 AM
  3. How to get only digits from an input string?
    By Luz in forum Software Development
    Replies: 4
    Last Post: 09-02-2010, 08:42 PM
  4. Java - converting long into string format
    By Kingfisher in forum Software Development
    Replies: 5
    Last Post: 09-01-2010, 02:50 PM
  5. Converting string to char
    By Abdullah30 in forum Software Development
    Replies: 3
    Last Post: 03-09-2009, 11:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,285,252.83297 seconds with 16 queries