c# - Scan string for numbers -


There is a string with my words and numbers. How can I get the final number (number of digits is unknown) and Can you separate it? So far I've used the substring method, but the location of the number in the text, and the length of the number is unknown.

Thank you!

Here's a simple solution:

  var number = Regex .Match (input, @ "(* * * \ D | ^) (\ d +)") group [2] .Value.  

Or a little different solution for a good solution:

  var number = Regex.Matches (input, @ ".d +" ) Cast & lt; Match & gt; () last price;  

And here is a solution using a plain old for loop ; No Linq or regex required:

  int start = 0, end = 0; (Var i = input.Length - 1; i> = 0; i--) {if (char.IsDigit (input [i])) {end = i + 1; Whereas (i> = 0 & four. IiD Digit (input [i])) i--; Start = i + 1; break; }} Var number = input.Substring (start, end-start);  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -