C# C_uu使用长度将字符串添加到字符串中

C# C_uu使用长度将字符串添加到字符串中,c#,arrays,if-statement,string-length,C#,Arrays,If Statement,String Length,在我的程序中,我试图为长度超过3的数字添加一个逗号。例如,如果数字是1000,它应该输出1000。 然而,在我把逗号放进第一个数字后,我不知道如何添加剩余的数字。下面的代码是我得到的: // if the answer is more than 999 string answerThousand = Convert.ToString(lbl_NumResult.Text); if (answerThousand.Length > 3) { lbl_NumResult.Text

在我的程序中,我试图为长度超过3的数字添加一个逗号。例如,如果数字是1000,它应该输出1000。 然而,在我把逗号放进第一个数字后,我不知道如何添加剩余的数字。下面的代码是我得到的:

// if the answer is more than 999
string answerThousand = Convert.ToString(lbl_NumResult.Text);

if (answerThousand.Length > 3) 
{
    lbl_NumResult.Text = answerThousand[1] + "," + answerThousand[ /* What must be here to add remaining numbers? */];
}

您可以将格式化程序传递给ToString方法:


为什么答案是[1],而不是[0]?字符串有基于零的索引,这是我想在第一个数字后面加逗号的地方。您可以使用带有N的String.Format,或字符串插值,如lbl_NumResult.Text=${answer000:N};或者n0对不起,我完全忘记了0是起始值,而不是1。
decimal inputValue = 0;
if (decimal.TryParse(lbl_NumResult.Text, out inputValue))
{
    string answerThousand = inputValue.ToString("N", CultureInfo.InvariantCulture);
}