Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Converting a string number into sequence of digits

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-03-2010
Member
 
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));
    }
Reply With Quote
  #2  
Old 13-03-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
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'; });
Reply With Quote
  #3  
Old 13-03-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
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();
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 13-03-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
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');
}
Reply With Quote
  #5  
Old 13-03-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
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()));
}
Reply With Quote
  #6  
Old 13-03-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
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));
        }
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Converting a string number into sequence of digits"
Thread Thread Starter Forum Replies Last Post
how to increase the number of decimal digits in JAVA program output? Dennis Racket Software Development 2 3 Weeks Ago 02:38 PM
Converting Boolean values to string in Java 27lynx27 Software Development 1 13-02-2011 03:40 AM
How to get only digits from an input string? Luz Software Development 4 09-02-2010 08:42 PM
Java - converting long into string format Kingfisher Software Development 5 09-01-2010 02:50 PM
Converting string to char Abdullah30 Software Development 3 04-09-2009 12:09 AM


All times are GMT +5.5. The time now is 03:53 AM.