我有很多数字字符串.我需要重新格式化字符串以在所有数字序列后添加逗号.数字有时可能包含其他字符,包括12-3或12/4,例如
谢谢你们
编辑: 我的例子没有考虑任何特殊字符.我最初没有把它包括在内,因为我认为如果有人能更有效地做到这一点,我会得到一个全新的视角 - 我的坏!
private static string CommaAfterNumbers(string input)
{
string output = null;
string[] splitBySpace = Regex.Split(input, " ");
foreach (string value in splitBySpace)
{
if (!string.IsNullOrEmpty(value))
{
if (int.TryParse(value, out int parsed))
{
output += $"{parsed},";
}
else
{
output += $"{value} ";
}
}
}
return output;
}
Run Code Online (Sandbox Code Playgroud)