2

I have value like below

string value = "11,.Ad23";
int n;
bool isNumeric = int.TryParse(value, out n);

I control if string is numeric or not.If string is not numeric and has non numeric i need to get non numeric values as below

Result must be as below

,.Ad

How can i do this in c# ?

1
  • What if the non numeric parts of the string are not consecutive like ABC123DEF. Would you want to get ABCDEF in that case? Commented Apr 29, 2015 at 12:59

4 Answers 4

9

If it doesn't matter if the non-digits are consecutive, it's simple:

string nonNumericValue = string.Concat(value.Where(c => !Char.IsDigit(c)));

Online Demo: http://ideone.com/croMht

If you use .NET 3.5. as mentioned in the comment there was no overload of String.Concat (or String.Join as in Dmytris answer) that takes an IEnumerable<string>, so you need to create an array:

string nonNumericValue = string.Concat(value.Where(c => !Char.IsDigit(c)).ToArray());

That takes all non-digits. If you instead want to take the middle part, so skip the digits, then take all until the the next digits:

string nonNumericValue = string.Concat(value.SkipWhile(Char.IsDigit)
                                            .TakeWhile(c => !Char.IsDigit(c)));
Sign up to request clarification or add additional context in comments.

Comments

3

Regular expression solution (glue together all non-numeric values):

  String source = "11,.Ad23";
  String result = String.Join("", Regex
    .Matches(source, @"\D{1}")
    .OfType<Match>()
    .Select(item => item.Value));

Edit: it seems that you use and old version of .Net, in that case you can use straightforward code without RegEx, Linq etc:

  String source = "11,.Ad23";

  StringBuilder sb = new StringBuilder(source.Length);

  foreach (Char ch in source)
    if (!Char.IsDigit(ch))
      sb.Append(ch);

  String result = sb.ToString();

4 Comments

why not string.Concat() ?
I copy and paste your code to .net paltform and it displays error as "the best overloaded method match for 'string.Join(string , string[])' has some invalid arguments"
@fubo: often (when glueing together) we need a separator, say "enumerate all the non-numeric symbols separating them by comma" so I've put Join. Nothing wrong with String.Concat() it's IMHO a question of taste.
@Richard: then it seems that you are not using NET 4.5 as mentioned in a comment to my answer but .NET 3.5. What visual studio version are you using, what compiler setting is active in the project as target framework? So either use at least .NET 4 or use ToArray to pass a string[] to String.Join(this answer) or String.Concat(my answer).
2

Although I like the solution proposed I think a more efficent way would be using regular expressions such as

[^\D]

Which called as

var regex = new Regex(@"[^\D]");
var nonNumeric = regex.Replace("11,.Ad23", "")); 

Which returns:

,.Ad

Comments

1

Would a LINQ solution work for you?

string value = "11,.Ad23";
var result = new string(value.Where(x => !char.IsDigit(x)).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.