How do I replace a specific occurrence of a string in a string?

后端 未结 7 1526
野的像风
野的像风 2020-12-18 03:12

I have a string which may contain \"title1\" twice in it.

e.g.

server/api/shows?title1=its always sunny in philadelphia&title1=breaking ba

相关标签:
7条回答
  • 2020-12-18 03:45

    I found this link immediately on a google search.

    C# - indexOf the nth occurrence of a string?

    Get the IndexOf the first occurrence of the string.

    Use the returned IndexOf's startIndex +1 for the starting position of the second IndexOf.

    Substring it into two strings at the appropriate index of the "1" character.

    Concat it back together with the "2" character.

    0 讨论(0)
  • 2020-12-18 03:53

    This will only replace the second instance of title1 (and any subsequent instances) after the first:

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

    However, if there are more than 2 instances, it may not be what you want. It's a little crude, but you can do this to handle any number of occurrences:

    int i = 1;
    string output = Regex.Replace(input, @"title1", m => "title" + i++);
    
    0 讨论(0)
  • 2020-12-18 03:53

    You can use the regex replace MatchEvaluator and give it a "state":

    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;
    });
    

    The replace can be one-lined by using the postfixed ++ operator (quite unreadable).

    string callingUrl2 = Regex.Replace(callingURL, "title1=", x => found++ == 1 ? "title2=" : x.Value);
    
    0 讨论(0)
  • 2020-12-18 03:55

    You could perhaps make use of a negative lookahead:

    title1(?!.*title1)
    

    And replace with title2.

    See how it's working here.

    0 讨论(0)
  • 2020-12-18 03:57

    Here is a C# extension method I created for a similar task that may come in handy.

    internal static class ExtensionClass
    {
        public static string ReplaceNthOccurance(this string obj, string find, string replace, int nthOccurance)
        {
            if (nthOccurance > 0)
            {
                MatchCollection matchCollection = Regex.Matches(obj, Regex.Escape(find));
                if (matchCollection.Count >= nthOccurance)
                {
                    Match match = matchCollection[nthOccurance - 1];
                    return obj.Remove(match.Index, match.Length).Insert(match.Index, replace);
                }
            }
            return obj;
        }
    }
    

    Then you can use it with the following example.

    "computer, user, workstation, description".ReplaceNthOccurance(",", ", and", 3)
    

    Which will produce the following.

    "computer, user, workstation, and description"
    

    OR

    "computer, user, workstation, description".ReplaceNthOccurance(",", " or", 1).ReplaceNthOccurance(",", " and", 2)
    

    Will produce the below.

    "computer or user, workstation and description"
    

    I hope this helps someone else who had the same question.

    0 讨论(0)
  • 2020-12-18 04:02

    you can specify a count, and an index to start searching at

    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);
    
    0 讨论(0)
提交回复
热议问题