【问题标题】:How do I replace a specific occurrence of a string in a string?如何替换字符串中特定出现的字符串?
【发布时间】:2013-06-26 16:37:51
【问题描述】:

我有一个字符串,其中可能包含两次“title1”。

例如

server/api/shows?title1=费城总是阳光明媚&title1=坏坏...

我需要将单词“title1”的第二个实例更改为“title2”

我已经知道如何识别字符串中是否存在两个字符串实例。

int occCount = Regex.Matches(callingURL, "title1=").Count;

if (occCount > 1)
{
     //here's where I need to replace the second "title1" to "title2"
}

我知道我们可能可以在这里使用 Regex,但我无法在第二个实例上获得替换。谁能帮帮我?

【问题讨论】:

  • 如果title1这个词有3个实例,你需要把第三个改成title3吗?
  • 这对我来说听起来像是一个更广泛的问题。
  • 它永远不会有超过 2 个实例
  • 你知道那句老话NEVER SAY NEVER
  • 我已经检查以确保字符串永远不会超过 2 个实例。外部字符串建立在屏幕上的用户选择之上,我考虑了正确的选择......

标签: c# regex string


【解决方案1】:

这只会在第一个实例之后替换title1 的第二个实例(以及任何后续实例):

string output = Regex.Replace(input, @"(?<=title1.*)title1", "title2");

但是,如果实例超过 2 个,则可能不是您想要的。这有点粗略,但您可以这样做来处理任意数量的事件:

int i = 1;
string output = Regex.Replace(input, @"title1", m => "title" + i++);

【讨论】:

  • pswg,我知道我已经接受了您的回答,但我只是尝试做完全相同的事情,但是使用“episodetitle1”这个词,它并没有给我相同的结果。愿意伸出援手吗? callURL = Regex.Replace(callingURL, @"(?
  • @Rj。输入字符串是什么?
  • 类似这样的东西:server/api/Shows?title1=break%20bad&title2=two%20and%20a%20half%20men&episodetitle1=pilot&episodetitle2=a%20big%20bag%20of%20dog
  • @Rj。好吧,看起来该字符串已经将第二个episodetitle1 替换为episodetitle2,但无论如何,无论您使用title1 还是episodetitle1,该模式都适用于我。
【解决方案2】:

您可以使用正则表达式替换 MatchEvaluator 并给它一个“状态”:

string callingURL = @"server/api/shows?title1=its always sunny in philadelphia&title1=breaking bad";

int found = -1;
string callingUrl2 = Regex.Replace(callingURL, "title1=", x =>
{
    found++;
    return found == 1 ? "title2=" : x.Value;
});

使用后缀++ 运算符可以单行替换(非常不可读)。

string callingUrl2 = Regex.Replace(callingURL, "title1=", x => found++ == 1 ? "title2=" : x.Value);

【讨论】:

    【解决方案3】:

    您可以指定计数和开始搜索的索引

    string str = @"server/api/shows?title1=its always sunny in philadelphia&title1=breaking bad ...";
    
    Regex regex = new Regex(@"title1");
    str = regex.Replace(str, "title2", 1, str.IndexOf("title1") + 6);
    

    【讨论】:

    • 这是一个很好的方法,但你为什么要在最后一步使用正则表达式呢?你可以简单地做一个string.Replace
    • @p.s.w.g string.Replace 是否有一个字段指定您开始搜索的位置?
    • 不,你是对的。您必须先做几个string.Substring(就像一个答案一样,现在已被删除)。
    • 正是我所需要的——在静态 API 中找不到这个。我很失望同名的静态方法缺少这个基本功能;它欺骗了我,让我相信 API 缺少它。恕我直言,如果您要使用不同的功能集重新实现静态/非静态方法,则不应重载方法名称。
    【解决方案4】:

    您也许可以使用负前瞻:

    title1(?!.*title1)
    

    并替换为title2

    看看它是如何工作的here

    【讨论】:

    • @Anirudh 用户已经通过if 确定有两个title1 实例。
    • 它只能出现一次
    【解决方案5】:

    我立即在谷歌搜索中找到了这个链接。

    C# - indexOf the nth occurrence of a string?

    获取第一次出现的字符串的IndexOf。

    使用返回的IndexOf的startIndex +1作为第二个IndexOf的起始位置。

    在“1”字符的适当索引处将其子串成两个字符串。

    将它与“2”字符重新连接起来。

    【讨论】:

      【解决方案6】:

      P.S.W.G 的做法真的很棒。但在下面我提到了一种简单的方法来为那些在 lambda 和正则表达式中遇到问题的人完成它..;)

      int index = input.LastIndexOf("title1=");

      string output4 = input.Substring(0, index - 1) + "&title2" + input.Substring(index + "title1".Length, input.Length - index - "title1".Length);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-09
        • 2021-10-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多