Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LINQ to parse the numbers from a string

Is it possible to write a query where we get all those characters that could be parsed into int from any given string?

For example we have a string like: "$%^DDFG 6 7 23 1"

Result must be "67231"

And even slight harder: Can we get only first three numbers?

like image 461
iLemming Avatar asked Aug 13 '10 20:08

iLemming


People also ask

Can you use LINQ on a string?

LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions. For example, you can use the String.

How do you find a number in a string?

To find numbers from a given string in Python we can easily apply the isdigit() method. In Python the isdigit() method returns True if all the digit characters contain in the input string and this function extracts the digits from the string. If no character is a digit in the given string then it will return False.

What is any () in LINQ?

The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.


1 Answers

public static string DigitsOnly(string strRawData)
  {
     return Regex.Replace(strRawData, "[^0-9]", "");
  }
like image 194
THines01 Avatar answered Sep 23 '22 02:09

THines01