Dieser Artikel wurde manuell übersetzt. Bewegen Sie den Mauszeiger über die Sätze im Artikel, um den Originaltext anzuzeigen. |
Übersetzung
Original
|
Convert.ToInt32-Methode (String)
Konvertiert die angegebene Zeichenfolgendarstellung einer Zahl in die entsprechende 32-Bit-Ganzzahl mit Vorzeichen.
Assembly: mscorlib (in mscorlib.dll)
Parameter
- value
- Typ: System.String
Eine Zeichenfolge, die die zu konvertierende Zahl enthält.
Rückgabewert
Typ: System.Int32Eine 32-Bit-Ganzzahl mit Vorzeichen, die der Zahl in value entspricht, oder 0 (null), wenn value gleich null ist.
| Ausnahme | Bedingung |
|---|---|
| FormatException |
value besteht nicht aus einem optionalen Vorzeichen und einer Folge von Ziffern (0 bis 9). |
| OverflowException |
value stellt eine Zahl dar, die kleiner als Int32.MinValue oder größer als Int32.MaxValue ist. |
Das Ergebnis des Aufrufs der Int32.Parse-Methode für value.
Wenn Sie eine Ausnahme nicht behandeln möchten, falls bei der Konvertierung ein Fehler auftritt, können Sie stattdessen die Int32.TryParse-Methode aufrufen. Sie gibt einen Boolean-Wert zurück, der angibt, ob die Konvertierung erfolgreich war oder nicht.
Das folgende Beispiel versucht, jedes Element in einem Array aus numerischen Zeichenfolgen in eine Ganzzahl zu konvertieren.
string[] values = { "One", "1.34e28", "-26.87", "-18", "-6.00", " 0", "137", "1601.9", Int32.MaxValue.ToString() }; int result; foreach (string value in values) { try { result = Convert.ToInt32(value); Console.WriteLine("Converted the {0} value '{1}' to the {2} value {3}.", value.GetType().Name, value, result.GetType().Name, result); } catch (OverflowException) { Console.WriteLine("{0} is outside the range of the Int32 type.", value); } catch (FormatException) { Console.WriteLine("The {0} value '{1}' is not in a recognizable format.", value.GetType().Name, value); } } // The example displays the following output: // The String value 'One' is not in a recognizable format. // The String value '1.34e28' is not in a recognizable format. // The String value '-26.87' is not in a recognizable format. // Converted the String value '-18' to the Int32 value -18. // The String value '-6.00' is not in a recognizable format. // Converted the String value ' 0' to the Int32 value 0. // Converted the String value '137' to the Int32 value 137. // The String value '1601.9' is not in a recognizable format. // Converted the String value '2147483647' to the Int32 value 2147483647.
Windows 7, Windows Vista SP1 oder höher, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core wird nicht unterstützt), Windows Server 2008 R2 (Server Core wird mit SP1 oder höher unterstützt), Windows Server 2003 SP2
.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.