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:
然后我这样替换了TnaName:
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:
这是可行的,但是当TnaName与我的文件夹名称相同时,我有一个问题。当这种情况发生时,我得到的是这样的一个字符串:
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? Thanks.
现在已经用feb11取代了TnaName的出现。有没有一种方法可以只替换字符串中最后出现的单词?谢谢。
Note: feb11
is TnaName
which comes from another process - that's not a problem.
注意:feb11是来自另一个过程的TnaName——这不是问题。
5 个解决方案
#1
119
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. - Source是要在其上执行操作的字符串。
-
Find
is the string that you want to replace. - 查找是要替换的字符串。
-
Replace
is the string that you want to replace it with. - 替换是要替换的字符串。
#2
11
Use string.LastIndexOf()
to find the index of the last occurrence of the string and then use substring to look for your solution.
使用string. lastindexof()查找字符串最后出现的索引,然后使用子字符串查找解决方案。
#3
7
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);
#4
2
I don't see why Regex can't be used:
我不明白为什么Regex不能使用:
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
#5
-1
You can use a Path
class from System.IO
namepace:
您可以使用来自System的Path类。IO namepace:
string filePath = "F:/jan11/MFrame/Templates/feb11";
Console.WriteLine(System.IO.Path.GetDirectoryName(filePath));
#1
119
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. - Source是要在其上执行操作的字符串。
-
Find
is the string that you want to replace. - 查找是要替换的字符串。
-
Replace
is the string that you want to replace it with. - 替换是要替换的字符串。
#2
11
Use string.LastIndexOf()
to find the index of the last occurrence of the string and then use substring to look for your solution.
使用string. lastindexof()查找字符串最后出现的索引,然后使用子字符串查找解决方案。
#3
7
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);
#4
2
I don't see why Regex can't be used:
我不明白为什么Regex不能使用:
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
#5
-1
You can use a Path
class from System.IO
namepace:
您可以使用来自System的Path类。IO namepace:
string filePath = "F:/jan11/MFrame/Templates/feb11";
Console.WriteLine(System.IO.Path.GetDirectoryName(filePath));