简体   繁体   English

字符串:替换字符串中的最后一个“ .something”?

[英]String: replace last “.something” in a string?

I have some string and I would like to replace the last .something with a new string. 我有一些字符串,我想用新字符串替换最后的.something。 As example: 例如:

string replace = ".new";
blabla.test.bla.text.jpeg => blabla.test.bla.text.new
testfile_this.00001...csv => testfile_this.00001...new

So it doesn't matter how many ..... there are, I'd like to change only the last one and the string what after the last . 因此,有多少个.....都无所谓,我只想更改最后一个,而在最后一个之后更改字符串. is coming. 来了。

I saw in C# there is Path.ChangeExtension but its only working in a combination with a File - Is there no way to use this with a string only? 我在C#中看到有Path.ChangeExtension,但是它只能与File一起使用-是否没有办法仅将其与字符串一起使用? Do I really need regex? 我真的需要正则表达式吗?

You can use string.LastIndexOf('.'); 您可以使用string.LastIndexOf('.');

string replace = ".new"; 
string test = "blabla.test.bla.text.jpeg";
int pos = test.LastIndexOf('.');
if(pos >= 0)
    string newString = test.Substring(0, pos-1) + replace;

of course some checking is required to be sure that LastIndexOf finds the final point. 当然,需要进行一些检查以确保LastIndexOf找到终点。

However, seeing the other answers, let me say that, while Path.ChangeExtension works, it doesn't feel right to me to use a method from a operating system dependent file handling class to manipulate a string. 但是,看到其他答案,我要说的是,尽管Path.ChangeExtension起作用了,但觉得使用操作系统依赖的文件处理类中的方法来操作字符串对我来说并不正确。 (Of course, if this string is really a filename, then my objection is invalid) (当然,如果此字符串确实是文件名,那么我的反对意见无效)

string replace = ".new";
string p = "blabla.test.bla.text.jpeg";
Console.WriteLine(Path.GetFileNameWithoutExtension(p) + replace);

Output: 输出:

blabla.test.bla.text.new

ChangeExtension should work as advertised; ChangeExtension应该按照广告中的说明工作;

string replace = ".new";
string file = "testfile_this.00001...csv";

file = Path.ChangeExtension(file, replace);

>> testfile_this.00001...new
string s = "blabla.test.bla.text.jpeg";
s = s.Substring(0, s.LastIndexOf(".")) + replace;

No you don't need regular expressions for this. 不,您不需要正则表达式。 Just .LastIndexOf and .Substring will suffice. 仅.LastIndexOf和.Substring就足够了。

string replace = ".new";
string input = "blabla.bla.test.jpg";

string output = input.Substring(0, input.LastIndexOf('.')) + replace;
// output = "blabla.bla.test.new"

Please use this function. 请使用此功能。

public string ReplaceStirng(string originalSting, string replacedString)
{
    try
    {
        List<string> subString = originalSting.Split('.').ToList();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < subString.Count - 1; i++)
        {
            stringBuilder.Append(subString[i]);
        }
        stringBuilder.Append(replacedString);
        return stringBuilder.ToString();
    }
    catch (Exception ex)
    {
        if (log.IsErrorEnabled)
            log.Error("[" + System.DateTime.Now.ToString() + "] " + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName + " :: " + System.Reflection.MethodBase.GetCurrentMethod().Name + " :: ", ex);
            throw;
    }
}

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

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