-4

I have one string having numbers and alphabets want to split alphabets and digits in separate array using LINQ query in C# .my string is as

"abcd 00001 pqr 003 xyz abc 0009"

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Uma Dixit
  • 3
  • 1
  • 5
  • 3
    What would be the expected output? And why do you want to use LINQ when you don't have to? – Mighty Badaboom Jul 14 '17 at 06:47
  • 1
    do you want the digits in one array and the letters in a second array? – Mong Zhu Jul 14 '17 at 06:48
  • I want string in separate array and digits in separate link – Uma Dixit Jul 14 '17 at 06:49
  • 1
    Have you tried anything yourself? Sounds like regularexpression would be nice here. – Esko Jul 14 '17 at 06:49
  • 1
    Why does it have to be LINQ? The Q stands for "Query Language", not manipulation – Marco Jul 14 '17 at 06:49
  • @UmaDixit Please add the exact expected result in your question. What does "digits in separate link" mean? – Mighty Badaboom Jul 14 '17 at 06:51
  • yes but it not using linq I want using linq var str = "abcd 00001 pqr 003 xyz abc 0009"; var regex = Regex.Match(str, @"([a-zA-Z]+)(\d+)"); var letters = regex.Groups[1].Value; var numbers = regex.Groups[2].Value; – Uma Dixit Jul 14 '17 at 06:51
  • @UmaDixit edit your question and add your comment to body of the question. Show what did you try and what you want achieve. To others pls do not down vote this question it's not bad, it's just not complete yet I hope – Marcin Jul 14 '17 at 06:56
  • so you don't need the single elements to be separated? just all characters in one array? – Mong Zhu Jul 14 '17 at 07:03

3 Answers3

3

you could transform the string to an char array and then use the Where clause to extract the necessary information:

string g =  "abcd 00001 pqr 003 xyz abc 0009";


char[] numbers = g.ToCharArray().Where(x => char.IsNumber(x)).ToArray();
char[] letters = g.ToCharArray().Where(x=> char.IsLetter(x)).ToArray();
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • Very elegant solution – DeadlyEmbrace Jul 14 '17 at 06:56
  • @DeadlyEmbrace thank you, but it strongly depends on what op expects as result. It might be useless for him, if he needs the single elements still separated as in the original string like "abcd" and "pqr" – Mong Zhu Jul 14 '17 at 06:59
1

You can do it in this way:

string a ="abcd 00001 pqr 003 xyz abc 0009";
var digits = a.Split().Where(x=> {double number; return double.TryParse(x,out number);});
var letters = a.Split().Where(x=> {double number; return !double.TryParse(x,out number);});
foreach(var a1 in digits)
{
    Console.WriteLine(a1);
}
foreach(var a1 in letters)
{
    Console.WriteLine(a1);
}

The idea is to try to Parse the character and if it is parsed successful then it's a number.

Samvel Petrosov
  • 7,580
  • 2
  • 22
  • 46
1

You can use GroupBy where the Key is a boolean that specifies if the entry is a number(Can be converted to a double) or text:

string input = "abcd 00001 pqr 003 xyz abc 0009";

double dummy;
var result = input.Split().GroupBy(i => double.TryParse(i, out dummy)).ToList();

var textArray = result.Where(i => !i.Key).SelectMany(i=> i).ToArray();
var numberArray = result.Where(i => i.Key).SelectMany(i => i.ToList()).ToArray();
Zein Makki
  • 29,485
  • 6
  • 52
  • 63