繁体   English   中英

替换字符串中最后一次出现的单词 - C#

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

我有一个问题,我需要替换字符串中最后一次出现的单词。

情况:我得到一个格式如下的字符串:

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

然后我像这样替换TnaName

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

这有效,但是当TnaName与我的folder name同时我TnaName了问题。 发生这种情况时,我最终会得到一个这样的字符串:

F:/feb11/MFrame/Templates/feb11

现在,它已经取代两次出现的TnaNamefeb11 有没有办法只替换字符串中最后一次出现的单词?

注意: feb11是来自另一个进程的TnaName - 这不是问题。

这是替换字符串最后一次出现的函数

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是您要对其进行操作的字符串。
  • Find是您要替换的字符串。
  • Replace是您要Replace的字符串。

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

您必须手动进行替换:

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

我不明白为什么不能使用正则表达式:

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

用法:

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

您可以使用System.IO命名空间中的Path类:

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

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

该解决方案可以通过一行更简单地实现:

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

因此,我们利用了正则表达式星号运算符的贪婪性。 该函数是这样使用的:

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

以下内容不会替换路径/字符串中的TnaName ,而是在不包含TnaName情况下返回它:

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

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

下面的函数分割一个字符串,其中模式(要替换的单词)最后出现。
然后它使用替换字符串(在字符串的后半部分)更改模式。
最后,它再次将两个字符串的一半重新连接起来。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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