How can I get all numbers (like int) from a string? FROM#

I have a long string, and I would like to skip it and pull out all the int values ​​in order. It seems simple enough, but I can't figure out how to figure it out.

string raw = "I am a string and I have some numbers 3 45 333 2 39 inside of me 1839 9303, and I'd like to get these numbers into 9 -10 00 9e09 into a string[] or something else"; int[] justNumbers = raw.????? 

Use C # .NET 3.5 and, if necessary, have access to Regex and Linq. Thanks.

The end result is a long list of ints. those.

 List<int> numbers = new List<int>(); 

WHAT I FINISHED TO USE (NOT THE MOST EFFECTIVE, BUT WORKS)

 #region mysolution numbers = new List<int>(); foreach (char item in raw) { if (item.ToString() == "0") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "1") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "2") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "3") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "4") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "5") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "6") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "7") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "8") { numbers.Add(Convert.ToInt32(item.ToString())); } else if (item.ToString() == "9") { numbers.Add(Convert.ToInt32(item.ToString())); } } #endregion 
+4
source share
5 answers

Something along the lines: (untested)

 var items = raw.Split(' '); var integers = new List<int>(); foreach(var i in items){ int test = 0; if(int.TryParse(i, out test)){ integers.add(test); } } 

EDIT : There is an overload of TryParse that accepts, among other things, the bitwise comparison of System.Globalization.NumberStyles . With this overload, you can specify what types of whole lines it can accept ( AllowExponent is one of them), so I would assume I didn’t check that 9e09 would work. :)

Hope this helps!

+7
source

A regex-based approach would look something like this:

 Regex number = new Regex(@"-?\d+"); List<int> ints = number.Matches(raw) .Cast<Match>() .Select(m => Int32.Parse(m.Value)) .ToList(); 

However, this does not handle 9e09 if it means that it represents 9 x 10 ^ 9 - it will interpret it as two separate numbers, analyzed as 9 and 9.

+6
source

The Crazy Linq Way:

  private static IEnumerable<int> GetNumbers(string str) { foreach (var st in str.Split(new string[]{" "},StringSplitOptions.RemoveEmptyEntries) .Where(s => (s.ToCharArray() .All(c => Char.IsDigit(c))))) { yield return Convert.ToInt32(st); } } 
+3
source

What about:

 int[] xx = raw.Split( ' ' ).Where( ( s, o ) => Int32.TryParse( s, out o ) ).Select( p => Int32.Parse( p ) ).ToArray(); 
+2
source

In response to itowlson, if anyone is concerned about overflow, you can use tryparse in this scenario with a temporary variable:

 int tmp = 0; var result = (from m in new Regex(@"-?\d+").Matches(s).OfType<Match>() let doesParse = int.TryParse(m.Value, out tmp) where doesParse select tmp).ToList(); 
+2
source

Source: https://habr.com/ru/post/1300921/


All Articles