简体   繁体   中英

Replace the last occurrence of a word in a string - C#

I have a problem where I need to replace the last occurrence of a word in a string.

Situation: I am given a string which is in this format:

string filePath ="F:/jan11/MFrame/Templates/feb11";

I then replace TnaName like this:

filePath = filePath.Replace(TnaName, ""); // feb11 is TnaName

This works, but I have a problem when TnaName is the same as my folder name . When this happens I end up getting a string like this:

F:/feb11/MFrame/Templates/feb11

Now it has replaced both occurrences of TnaName with feb11 . Is there a way that I can replace only the last occurrence of the word in my string?

Note: feb11 is TnaName which comes from another process - that's not a problem.

Here is the function to replace the last occurrence of a string

public static string ReplaceLastOccurrence(string Source, string Find, string Replace)
{
        int place = Source.LastIndexOf(Find);

        if(place == -1)
           return Source;

        string result = Source.Remove(place, Find.Length).Insert(place, Replace);
        return result;
}
  • Source is the string on which you want to do the operation.
  • Find is the string that you want to replace.
  • Replace is the string that you want to replace it with.

使用string.LastIndexOf()查找字符串最后一次出现的索引,然后使用 substring 查找您的解决方案。

You have to do the replace manually:

int i = filePath.LastIndexOf(TnaName);
if (i >= 0)
    filePath = filePath.Substring(0, i) + filePath.Substring(i + TnaName.Length);

I don't see why Regex can't be used:

public static string RegexReplace(this string source, string pattern, string replacement)
{
  return Regex.Replace(source,pattern, replacement);
}

public static string ReplaceEnd(this string source, string value, string replacement)
{
  return RegexReplace(source, $"{value}$", replacement);
}

public static string RemoveEnd(this string source, string value)
{
  return ReplaceEnd(source, value, string.Empty);
}

Usage:

string filePath ="F:/feb11/MFrame/Templates/feb11";
filePath = filePath.RemoveEnd("feb11"); // F:/feb11/MFrame/Templates/
filePath = filePath.ReplaceEnd("feb11","jan11"); // F:/feb11/MFrame/Templates/jan11

You can use a Path class from System.IO namepace:

string filePath = "F:/jan11/MFrame/Templates/feb11";

Console.WriteLine(System.IO.Path.GetDirectoryName(filePath));

The solution can be implemented even more simple with a single line:

 static string ReplaceLastOccurrence(string str, string toReplace, string replacement)
    {
        return Regex.Replace(str, $@"^(.*){Regex.Escape(toReplace)}(.*?)$", $"$1{Regex.Escape(replacement)}$2");
    }

Hereby we take advantage of the greediness of the regex asterisk operator. The function is used like this:

var s = "F:/feb11/MFrame/Templates/feb11";
var tnaName = "feb11";
var r = ReplaceLastOccurrence(s,tnaName, string.Empty);

The following doesn't replace TnaName in the path/string, but instead returns it without TnaName included:

var lastIndex = filePath.LastIndexOf(TnaName); // get position of TnaName

filePath = filePath.Substring(0, lastIndex); // get path till position of TnaName

The following function splits a string where the pattern (word to be replaced) last occurs.
Then it changes the pattern with the replacement string (in the second half of the string).
Finally, it concatenates both string halves back with each other again.

using System.Text.RegularExpressions;

public string ReplaceLastOccurance(string source, string pattern, string replacement)
{
   int splitStartIndex = source.LastIndexOf(pattern, StringComparison.OrdinalIgnoreCase);
   string firstHalf = source.Substring(0, splitStartIndex);
   string secondHalf = source.Substring(splitStartIndex, source.Length - splitStartIndex);
   secondHalf = Regex.Replace(secondHalf, pattern, replacement, RegexOptions.IgnoreCase);

   return firstHalf + secondHalf;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM