替换c#中字符串中的字符
本文关键字:字符 字符串 替换 | 更新日期: 2023-09-27 17:55:02
我使用Readline()
从串行端口读取字符串。
但问题是字符串总是在末尾附加"'r"
。
我试过了
text.Replace("'r","");
但它不工作。
Replace
不能就地工作。你必须把结果赋值给一个变量。
text = text.Replace("'r","");
或
text = text.Trim();
您需要将结果赋值给某个字符串以获得没有'r
的字符串
text.Replace("'r","");
text = text.Replace("'r","");
转义'
,
text.Replace("''r","");
或使用@
,逐字字符串
text.Replace(@"'r","");
使用这个代替,因为回车符根据当地文化的不同而不同:
text.Replace(Environment.NewLine, "");
尝试使用@
verbtaim literal
like;
text.Replace(@"'r","");
或者你可以使用双斜杠(''
)
text.Replace("''r","");
'r
是回车字符字面量。查看Character literals
注意String.Replace()
方法,因为它有两个重载。
-
Replace(Char, Char)
-
Replace(String, String)
"…"字符串总是在"
"后面加上"'r"。然后去掉最后一个字符:string a = "hello'r";
string b = a.Substring(0, a.Length - 1);