すでにカンマ区切りの文字列を持っている場合は、アレイにコンマに基づいて、それを分割するString.Split()
メソッドを使用することができますし、それはそれぞれInt32.Parse()
またはConvert.ToInt32()
メソッドを使用して適切な整数だに、あなたはこれらの各値を変換することができます:
see an example of this in action hereです。
var output = input.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => Int32.Parse(s.Trim()))
.ToArray();
アンより安全なアプローチは、まだ適切にInt32.TryParse()
方法などを経て、整数として解析することができる唯一の使用値に次のようになります。あなたが明示的に空のエントリと空白可能性を無視するために必要な場合は、次の調整の例を使用することができます以下の図を参照してください。
// Split your string, removing any empty entries
var output = strings.Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries)
.Select(n => {
// A variable to store your value
int v;
// Attempt to parse it, store an indicator if the parse was
// successful (and store the value in your v parameter)
var success = Int32.TryParse(n, out v);
// Return an object containing your value and if it was successful
return new { Number = v, Successful = success };
})
// Now only select those that were successful
.Where(attempt => attempt.Successful)
// Grab only the numbers for the successful attempts
.Select(attempt => attempt.Number)
// Place this into an array
.ToArray();
"ベクター機能"とはどのようなものですか?それらの関数が必要とする型を教えてくれたら、その型を作成するコードを与えることができます。 – Quantic
ランダムな整数を生成する方法(あなたの目的には十分な擬似ランダムで十分ですか?)または既に持っている整数をベクトル形式に変換することについての質問はありますか?後であれば、どんなインプットがありますか? –
@Eric私はすでに文字列にランダムな整数を持っています。変換するメソッドが必要です。 – Sarah