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?
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.
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.
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.
public static string DigitsOnly(string strRawData)
{
return Regex.Replace(strRawData, "[^0-9]", "");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With