2016-08-02 20 views
2

Hier ist mein String mit 3 ganzen Zahlen und ich möchte es in 3 ganzzahligen Variablen speichern, aber ich kann keine Antwort finden.Konvertiere einen String in ein Integer-Array

string orders = "Total orders are 2222 open orders are 1233 closed are 222"; 

Dies ist, was ich tun möchte.

int total = 2222; 
int close = 222; 
int open = 1233; 
+1

die Zeichenfolge festgelegt ist, wird es immer sein "Total-Aufträge sind ### offene Aufträge sind ### ...."? –

Antwort

5

Versuchen Sie es mit regulären Ausdrücken (zu extrahieren Muster) und Linq (organisieren sie in int[]):

string orders = "Total orders are 2222 open orders are 1233 closed are 222"; 

    int[] result = Regex 
    .Matches(orders, "[0-9]+") 
    .OfType<Match>() 
    .Select(match => int.Parse(match.Value)) 
    .ToArray(); 
0

Sie jedes Zeichen wiederum prüfen und sehen, ob es sich um eine numerischer Wert:

string orders = "Total orders are 2222 open orders are 1233 closed are 222"; 

List<int> nums = new List<int>(); 
StringBuilder sb = new StringBuilder(); 

foreach (Char c in orders) 
{ 
    if (Char.IsDigit(c)) 
    { 
     //append to the stringbuilder if we find a numeric char 
     sb.Append(c); 
    } 
    else 
    { 
     if (sb.Length > 0) 
     { 
      nums.Add(Convert.ToInt32(sb.ToString())); 
      sb = new StringBuilder(); 
     } 
    } 
} 

if (sb.Length > 0) 
{ 
    nums.Add(Convert.ToInt32(sb.ToString())); 
} 

//nums now contains a list of the integers in the string 
foreach (int num in nums) 
{ 
    Debug.WriteLine(num); 
} 

Ausgang:

Hier
2222 
1233 
222 
1

ist ein Weg,

namespace StringToIntConsoleApp 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string orders = "Total orders are 2222 open orders are 1233 closed are 222"; 
      string[] arr = orders.Split(' '); 
      List<int> integerList = new List<int>(); 
      foreach(string aString in arr.AsEnumerable()) 
      { 
       int correctedValue ; 
       if(int.TryParse(aString,out correctedValue)) 
       { 
        integerList.Add(correctedValue); 
       } 
      } 

      foreach (int aValue in integerList) 
      { 
       Console.WriteLine(aValue); 
      } 
      Console.Read(); 
     } 
    } 
} 
4

Sie können es nur mit Linq:

int[] result = orders 
    .Split(' ') 
    .Where(s => s 
    .ToCharArray() 
    .All(c => Char.IsDigit(c))) 
    .Select(s => Int32.Parse(s)) 
    .ToArray(); 
+0

Ich schlage vor, 'Select' und' ToArray' hinzuzufügen, da Frage erfordert für * Integer-Array * –

+0

Thx. Ich habe die Antwort bearbeitet –

+0

Seien Sie vorsichtig mit 'Char.IsDigit', z. 'Char.IsDigit ('6');' gibt 'wahr' zurück; und 'int.Parse (" 6 ")' wird * Ausnahme auslösen *; genauer Test ist 'c> = '0' && c <= '9'' –

0

Ich würde es so machen:

var intArr = 
    orders.Split(new[] { ' ' }, StringSplitOptions.None) 
      .Select(q => 
        { 
         int res; 
         if (!Int32.TryParse(q, out res)) 
          return (int?)null; 
         return res; 
        }).Where(q => q.HasValue).ToArray(); 
-1
int[] results = orders.Split(' ').Where(s => s.ToCharArray().All(c => Char.IsDigit(c))) 
    .Select(s => Int32.Parse(s)).ToArray(); 
0

Diese extrahiert die Ganzzahlen aus dem String:

var numbers = 
    Regex.Split(orders, @"\D+") 
     .Where(x => x != string.Empty) 
     .Select(int.Parse).ToArray(); 

Ausgabe

numbers[0] == 2222 
numbers[1] == 1233 
numbers[2] == 222 
Verwandte Themen