[英]C#: Replace Certain Characters if they are the First Character of the String Using Regex
只有当它们是字符串的第一个字符时,我才需要用空格+相同的字符替换以下字符:
"-"
"+"
"="
Regex rgx = new Regex("^[-+=]");
string text = "+x" //your Text goes here
if (rgx.IsMatch(text))
{
text = " " + text;
}
其他方式
var startChars = new List<string> { "+", "-", "=" };
if(startChars.Contains(text.First())
{
text = $" {text.Substring(1,text.Length-1)}";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.