locked
string.slit method using a numeric character RRS feed

  • Question

  • Say I have a string that reads C8O12B54Na324Si16253...

    I want to split this string into two string arrays, one for the letters and one for the numbers.  In other words...

    letters [0] = C, letters [1] = O, letters [2] = B, letters [3] = Na, etc.

    numbers[0] = 8, numbers[1] = 12, numbers[2] = 54, etc.

    I know the string.split method is easy when there is a definite character like '+' or '/', but what do I use to represent any/all numeric character?

    Friday, September 6, 2013 9:03 PM

Answers

  • Ok.  I found a way to cast the results into an array of strings like you originally wanted:

                var numRegx = new Regex(@"\d+");
                var numbers = numRegx.Matches(input).Cast<Match>().Select(m => m.Value).ToArray();
    
                var wordRegx = new Regex(@"[a-zA-Z]+");
                var words = wordRegx.Matches(input).Cast<Match>().Select(m => m.Value).ToArray();


    • Edited by jrboddie Saturday, September 7, 2013 1:49 AM
    • Proposed as answer by Khant Nipun Saturday, September 7, 2013 4:21 AM
    • Marked as answer by RandyPete Saturday, September 7, 2013 10:04 AM
    Saturday, September 7, 2013 1:48 AM

All replies

  • Unfortunately, this does not work for my purpose.  This code is just separating each single character. 

    Suppose I have C8Na34Gag12.  I need my arrays to be

    letters[0] = C, letters[1] = Na, letters[2] = Gag

    numbers[0] = 8, numbers[1] = 34, numbers[2] = 12

     

    Right now, your code outputs something like

    letters[0] = C, letters[1] = N, letters[2] = a, etc.



    • Edited by RandyPete Friday, September 6, 2013 10:28 PM
    Friday, September 6, 2013 10:27 PM
  • That is why I deleted it.  Try this:

               var input = "C8O12B54Na324Si16253";
    
                var numRegx = new Regex(@"\d+");
                var numbers = numRegx.Matches(input);
    
                foreach (var item in numbers)
                {
                    Debug.WriteLine(item.ToString());
                }
    
                var wordRegx = new Regex(@"[a-zA-Z]+");
                var words = wordRegx.Matches(input);
    
                foreach (var item in words)
                {
                    Debug.WriteLine(item.ToString());
                }

    Friday, September 6, 2013 10:44 PM
  • Thank you very much for your prompt reply.  Unfortunately, my computer just got that moneypak virus where it holds your pc ransom and make you pay to unlock it.  Really annoying...  Typing this on my tablet.

    Doing a system restore right now.  Unbelievable...

     
    • Edited by RandyPete Friday, September 6, 2013 11:19 PM blah
    Friday, September 6, 2013 11:18 PM
  • I've restored. 

    Just tried your code.  "Debug" isn't recognized.  What do I need to include?

    Edit.

    Nevermind.  Got it.

    • Edited by RandyPete Saturday, September 7, 2013 12:07 AM
    Friday, September 6, 2013 11:36 PM
  • The Regex.Matches() method returns a collection of Match objects that contain the items you are looking for.

    You can access any individual item by index as in string n = number[2].ToString();

    I just used the loops and Debug.WriteLine to show the output in the Output window.


    • Edited by jrboddie Saturday, September 7, 2013 12:16 AM
    Saturday, September 7, 2013 12:15 AM
  • Ok.  I found a way to cast the results into an array of strings like you originally wanted:

                var numRegx = new Regex(@"\d+");
                var numbers = numRegx.Matches(input).Cast<Match>().Select(m => m.Value).ToArray();
    
                var wordRegx = new Regex(@"[a-zA-Z]+");
                var words = wordRegx.Matches(input).Cast<Match>().Select(m => m.Value).ToArray();


    • Edited by jrboddie Saturday, September 7, 2013 1:49 AM
    • Proposed as answer by Khant Nipun Saturday, September 7, 2013 4:21 AM
    • Marked as answer by RandyPete Saturday, September 7, 2013 10:04 AM
    Saturday, September 7, 2013 1:48 AM
  • Hey jrboddie, how should I modify this code for it to recognize negative numbers as well?  Right now, the negative signs just disappear.  I need it to recognize negative numbers as well.  Thanks.
    Wednesday, September 25, 2013 2:30 AM