gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:45 25 4
gpt4 key购买 nike

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

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, "'");

关于c# - 如何使用 string.Insert c# 在字符串中间插入撇号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26524298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com