OPがの使用を主張しているようにString.Format
:
string input; //the input of your textbox
int temp = int.Parse(input); //parse your input into an int
//the Format() adds the decimal points, the replace replaces them with the /
string output = String.Format("{0:0,0}", temp).Replace('.', '/');
ここで重要なステップは、テキスト ボックスのテキストを整数にキャストすることです。これにより、小数点の挿入が簡単になりString.Format()
ます。もちろん、解析時にテキストボックスが有効な数値であることを確認する必要があります。そうしないと、例外が発生する可能性があります。
編集
それで...動的な長さの数値があり、静的なフォーマット文字列を使用してそれをフォーマットしたいですか? 不可能だよ。どこかに書式文字列を作成する動的コードが必要です。
正規表現や文字列置換を再度参照することなく、入力番号に応じてフォーマット文字列を作成するコードを次に示します。
このようにして、String.Format()
呼び出しは 1 回だけです。おそらく、フォーマット文字列を作成するアルゴリズムを別の場所に配置して、必要な場所から呼び出すことができます。
string input; //the input of your textbox
int temp = int.Parse(input); //parse your input into an int
string customString = "{0:";
string tempS = "";
for (int i = 0; i < input.Length; i++)
{
if (i % 3 == 0 && i != 0)
{
tempS += "/";
}
tempS += "#";
}
tempS = new string(tempS.Reverse().ToArray());
customString += tempS;
customString += "}";
string output = String.Format(customString, temp));