2

I have String array:

String[] values  = {"A1","B1","C1","5"};

I need to separate digits and cell references like that:

String[] b = {"A1","B1","C1"}; 
String[] c = {"5"}; 
3
  • 1
    And what have you done fo far ? Commented Aug 5, 2015 at 9:42
  • 3
    Hint: ignore the array part... given a string, how would you work out whether or not it's a cell reference? Commented Aug 5, 2015 at 9:43
  • 2
    @ndn The question is tagged C#, so no. Commented Aug 5, 2015 at 9:46

3 Answers 3

3

Using linq it's pretty straightforward (except for the "number only string recognition"). Here it is:

static void Main(string[] args)
{

    // Input:
    String[] values = { "A1", "B1", "C1", "5" };

    // Results:
    String[] digits = (from x in values where StringContainsNumbersOnly(x) select x).ToArray();
    String[] cellRefs = (from x in values where !digits.Contains(x) select x).ToArray();

}

static bool StringContainsNumbersOnly(string inputString)
{
    return System.Text.RegularExpressions.Regex.IsMatch(inputString, @"^\d+$");
}
Sign up to request clarification or add additional context in comments.

3 Comments

and what about this varian: String[] values = { "A1", "B1", "C1", "5" , "string"}; To separate into three arrays?
I presume that for your example "string" it means "strings with no numbers". In this case, create a new function StringContainsNoNumbers, just as in StringContainsNumbersOnly but i in this case return Regex.IsMatch(inputString, @"^[a-zA-Z]+$");. Then you use String[] noNumbers = (from x in values where StringContainsNoNumbers(x) select x).ToArray(); and you are good to go.
As you can see, the whole point consists in doing "select x where x matches the desired case". So you can just google "c# string contains no numbers" or whatever your desired case is, and plug it in your "select" statement.
1

If your definition of a cell reference is a string starting with a letter you can do something like:

List<string> cellReferences = new List<string>();
List<string> digits = new List<string>();
String[] values  = {"A1","B1","C1","5"};

foreach(string val in values)
{
  if(Char.IsLetter(val[0]))
    cellReferences.Add(val);
  else
    digits.Add(val);
}

Comments

1

A simple linq is enough...

String[] values = { "A1", "B1", "C1", "5" };

var groups = values.ToLookup(s => s.All(char.IsDigit));

String[] b = groups[false].ToArray();
String[] c = groups[true].ToArray();

EDIT

And for your tri-state case

and what about this varian: String[] values = { "A1", "B1", "C1", "5" , "string"}; To separate into three arrays?

String[] values = { "A1", "B1", "C1", "5", "string" };

var groups = values.ToLookup(s => s.All(char.IsDigit) ? 1 : s.All(char.IsLetter) ? -1 : 0);

String[] alldigit = groups[1].ToArray();
String[] allLetter= groups[-1].ToArray();
String[] mixed    = groups[0].ToArray();

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.