2

I have a string variable that contains csv value like this:

string str = "105, c#, vb, 345, 53, sql51";

so now i want to get only alphanumeric items in a list or array without using loop.

required result:

string result = "c#, vb, sql51";

Or in list Or in array...

5
  • I have tried to split using this: string[] strArray = str.split(','); and then loop one by one and check is numeric.. Commented Jun 5, 2013 at 9:33
  • What do you mean by 'without using loop'? Commented Jun 5, 2013 at 9:33
  • 105 is alphanumeric... Did you mean "get only substrings which are not numbers"? Commented Jun 5, 2013 at 9:35
  • You're considering wrongly "alphanumeric" meaning. It doesn't mean "strict combination of letters and numbers", but "allows combination of letters and numbers, but not special characters". So "c#" it's not allowed while the others are accepted. Commented Jun 5, 2013 at 9:35
  • 1
    C# is not alphanumeric. Commented Jun 5, 2013 at 9:42

4 Answers 4

5
string str = "105, c#, vb, 345, 53, sql51";
var separator = ", ";
int dummy;

var parts = str.Split(new[]{separator}, StringSplitOptions.RemoveEmptyEntries)
               .Where(s => !int.TryParse(s, out dummy));

string result = string.Join(separator, parts);

Console.WriteLine(result);

prints:

c#, vb, sql51
Sign up to request clarification or add additional context in comments.

4 Comments

Your solution is wrong. "C#" is not alphanumeric since "#" character is not allowed while "105" does.
@dasblinkenlight if the question is unclear, how can we provide an answer :) And, What about "105.0"? It's a string or a number (not an Integer, but a number anyway)? It fits with OP conception of "alphanumeric".
@FrancescoDeLisi "C#" is not alphanumeric - this is a question to the OP, because it does appear in the result set.
@dasblinkenlight I agree, but what about "105.0" in your algorithm? It will be part of "parts"? Is it numeric or not?
4

Split using the Split method, filter with a LINQ expression, and call ToArray or ToList on the result to produce a filtered array:

var res = str
    .Split(new[] {',', ' '})
    .Where(s => s.Any(c => !Char.IsDigit(c)))
    .ToList();

Demo on ideone.

3 Comments

Upvoted this one since this encourages the question asker to do his homework instead of chewing it out for him. -edit: and undone.
how?? can u please explain with example?
Yeah, the result sample is a string but something else is OK too.
0

Something like:

var str = "test,test,tes,123,5";
var result = string.Join(",", str.Split(',').Where(s => !s.All(t => Char.IsNumber(t))));
result.Dump();

Comments

0
"105, c#, vb, 345, 53, sql51".Split(",")
       .Where(item => new Regex("[#A-Za-z0-9]").IsMatch(item))
           .Select(item=> item.Trim())
                 .ToList();

Note: Not sure why the OP wants the numbers filtered out-- Numbers are alphanumeric.

4 Comments

Please, consider strings with numbers only.
You forgot to support the # (and all the other non-numeric characters that might be present)
@MatthewWatson thanks-- still a bit confused about what results OP expects to get. Assuming # is acceptable, aren't all the numbers in that list alphanumeric?
@ek_ny Yes, agreed - I think it's not well specified in the OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.