Skip to main content
 首页 » 编程设计

c#之如何使用 string.Insert c# 在字符串中间插入撇号

2024年02月06日21grandyang

我有一堆循环遍历的字符串。我想在任何带有撇号的字符串中插入一个撇号。我只是做了类似下面的事情。

string strStatus =  "l'oreal"; 
 
index = strStatus.IndexOf("'"); 
strStatus.Insert(index, "  '  "); 

我想要像欧莱雅那样的输出。然而这失败了。我尝试使用转义模式

   strStatus.Insert(index, "  \'  "); 

一切都无济于事。请问我如何实现这一目标?非常感谢任何建议/帮助。

请您参考如下方法:

字符串是不可变的。 Insert 返回一个带有 2 个撇号的新字符串,它不会以任何方式修改 strStatus。您的代码只是丢弃 Insert 的结果。

你应该尝试: string strStatus = "欧莱雅";

index = strStatus.IndexOf("'"); 
string newStatus=strStatus.Insert(index, "'");