C#字符串替换
我想用一个替换“,”; 在我的字符串中。
例如:
改变这个
“文本”,“文本”,“文本”,
对此
“文本,文本,文本”,
我一直在尝试使用line.replace( ... , ... )
但无法正常工作。
任何帮助,将不胜感激。
你试过这个:
line.Replace("\",\"", ";")
你需要在搜索字符串中转义双引号,如下所示:
string orig = "\"Text\",\"Text\",\"Text\""; string res = orig.Replace("\",\"", ";");
请注意,替换不会“就地”发生,因为.NET字符串是不可变的 。 通话后原始字符串将保持不变; 只有返回的字符串res
才会有替换。
var str = "Text\",\"Text\",\"Text"; var newstr = str.Replace("\",\"",";");
最简单的方法就是做
line.Replace(@",", @";");
输出如下所示:
line.Replace(@""",""", ";");
怎么line.Replace(@""",""", ";");
确保你正确地逃脱了报价。
string line = "\"Text\",\"Text\",\"Text\","; string result = line.Replace("\",\"", ";");
你不能使用string.replace..as分配一个字符串,你不能操纵。因为我们使用字符串builder.here是我的例子。在html页面我添加[Name],由Name.make替换,确保[Name]是唯一的或者你可以给出任何独特的名字
string Name = txtname.Text; string contents = File.ReadAllText(Server.MapPath("~/Admin/invoice.html")); StringBuilder builder = new StringBuilder(contents); builder.Replace("[Name]", Name); StringReader sr = new StringReader(builder.ToString());
//Replace Method Here I'm replace old value to new value string actual = "Hello World"; string Result = actual.Replace("World", "stackoverflow"); ---------------------- Output : "Hello stackoverflow"