1

I have so expression that contains numbers and plus symbols:

string expression = 235+356+345+24+5+2+4355+456+365+356.....+34+5542;
List<string>  numbersList = new List<string>();

How should I extract every number substring (235, 356, 345, 24....) from that expression and collect them into a string list?

1

4 Answers 4

4

You can do something like

List<string> parts = expression.Split('+').ToList();

http://msdn.microsoft.com/en-us/library/system.string.split.aspx

If there is any potential for white space around the + signs, you could so something a little more fancy:

List<string> parts = (from t in expression.Split('+') select t.Trim()).ToList();
Sign up to request clarification or add additional context in comments.

Comments

3

Something like:

string expression = "235+356+345+24+5+2+4355+456+365+356";
List<string> list = new List<string>(expression.Split('+'));

Comments

1

Try this piece of code

string expression = "235+356+345+24+5+2+4355+456+365+356";
string[] numbers = expression.Split('+');
List<string> numbersList = numbers.ToList();

2 Comments

You don't need the new char[] { '+' }. Just '+' works fine and is more compact.
Works because it's defined as a params parameter.
1

Or this, a positive check for numeric sequences:

private static Regex rxNumber = new Regex( "\d+" ) ;
public IEnumerable<string> ParseIntegersFromString( string s )
{
    Match m = rxNumber.Match(s) ;
    for ( m = rxNumber.Match(s) ; m.Success ) ; m = m.NextMatch() )
    {
        yield return m.Value ;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.