Sprache: C#
Methode gibt ein [b]int Array[/b] zurück.
Übergabeparameter ist ein [b]string[/b].
In der Methode wird per [b]Regex[/b] geprüft ob es sich um einen rein nummerischen "string" handelt.
/// <summary>
/// Methode nimmt einen string entgegen.
/// <para>Der string wird per <see cref="Regex.IsMatch(string)"/> auf nur nummerische elemente getestet.</para>
/// <para>Wenn <code>true</code> fülle das int Array, wenn <code>false</code> werfe eine <see cref="FormatException"/></para>
/// </summary>
/// <param name="input">string value </param>
/// <returns>int array</returns>
public static int[] StringToIntArray(string input)
{
if (string.IsNullOrEmpty(input)) // check input
throw new NullReferenceException("input can´t be null"); // throw nullpointer ex if input was null
var intArr = new int[input.Length]; // create int array with the length of input string
var pattern = "^[0-9]*$"; // only number allowed ( regex pattern string )
if (System.Text.RegularExpressions.Regex.IsMatch(input, pattern)) // check it with regex
{
for(int i = 0; i < input.Length;) // for loop to fill int[]
{
intArr[i] = Convert.ToInt16(input[i].ToString()); // fill it
i++;
}
}
else
{
throw new FormatException("only numbers allowed, please check your input string");
}
return intArr; // return
}
/// <summary>
/// Methode nimmt einen string entgegen.
/// <para>Der string wird per <see cref="Regex.IsMatch(string)"/> auf nur nummerische elemente getestet.</para>
/// <para>Wenn <code>true</code> fülle das int Array, wenn <code>false</code> werfe eine <see cref="FormatException"/></para>
/// </summary>
/// <param name="input">string value </param>
/// <returns>int array</returns>
public static int[] StringToIntArray(string input)
{
if (string.IsNullOrEmpty(input)) // check input
throw new NullReferenceException("input can´t be null"); // throw nullpointer ex if input was null
var intArr = new int[input.Length]; // create int array with the length of input string
var pattern = "^[0-9]*$"; // only number allowed ( regex pattern string )
if (System.Text.RegularExpressions.Regex.IsMatch(input, pattern)) // check it with regex
{
for(int i = 0; i < input.Length;) // for loop to fill int[]
{
intArr[i] = Convert.ToInt16(input[i].ToString()); // fill it
i++;
}
}
else
{
throw new FormatException("only numbers allowed, please check your input string");
}
return intArr; // return
}
Alte URL:
/snippet/string-to-int-array/15155
Regex ist sehr langsam im Vergleich zu int.TryParse oder einem einfachen Try-Catch-Block der eine Parsing Exception abfangen würde. Auch das häufige Aufrufen von ToInt16 finde ich nicht sonderlich effizient. Und warum parst du auf In16, gibst ein Int32 Array zurück, wenn jeweils Byte reichen würde?
Ich hätte es vermutlich als LinQ-Artige Methode geschrieben. In etwa so (ungetestet): StringToIntArray(string input)
[code]private IEnumerable
{
return input.Select(x =>
{
if (x < '0' || x > ‚9‘)
{
throw new ArgumentOutOfRangeException(„Only digits are valid characters“);
}
return (byte)(x-‚0‘);
});
}[/code]Ein .ToArray() hinten dran würde ein Array draus machen.
Oder aber man nutzt kein LinQ, aber die selbe Logik:
[code]private byte[] StringToIntArray(string input)
{
var result = new byte[input.Length];
for (int i = 0; i < input.Length; ++i) { var x = input[i]; if (x < '0' || x > ‚9‘)
{
throw new ArgumentOutOfRangeException(„Only digits are valid characters“);
}
result[i] = (byte)(x-‚0‘);
};
return result;
}[/code]
Hi,
Danke für deine Kritik.
Bin neu in C# und in ein paar Wochen würde ich es wahrscheinlich auch anders machen, aber der Code macht im moment das was er soll…
Gruss Thomas
Das der Code funktioniert ist die Hauptsache. Ich hätte in meinen Anfängen gerne jemanden gehabt der mir derartige Kritik gibt, daher mein Kommentar.
Dafür bin ich dir auch dankbar!
Je mehr Input(auch Kritik jeglicher Art) desto besser.
Man zieht dann komplett andere Faktoren mitein wenn man den nötigen Wissensstand hat.
Danke nochmal..