删除从文本文件某些字符串的最后一个实例,而不改变字符串的其他实例 [英] Delete the last instance of a certain string from a text file without changing the other instances of the string

查看:190
本文介绍了删除从文本文件某些字符串的最后一个实例,而不改变字符串的其他实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里我使用了很多RegEx.Replace,以取代在我的文本文件中的文本C#程序。



下面是我的问题。



在我的文本文件,我有一个代码,如M6T1。此代码是列在文本文件中的许多地方。



不过,我只希望在文本文件中从底部(最后一个实例),将其删除。总会有一个M6T1的文本文件的底部,但它并不总是最后一行。这可能是从底部3号线,从底部等5日线。



我只是想摆脱M6T1,所以正则表达式的最后一个实例。更换不会在这里工作。我不想跟其他的M6T1记者在文本文件中的其他位置地干扰。



可有人请给我一个解决这个问题?



感谢


解决方案

  VAR针=M6T1; 
无功九= str.LastIndexOf(针);
海峡= str.Substring(0,九)+ str.Substring(IX + needle.Length);


I have a C# program where I am using a lot of RegEx.Replace to replace text in my text file.

Here is my problem.

In my text file, I have a code such as "M6T1". This code is listed in numerous places in the text file.

However, I only want to delete it from the bottom (last instance) in the text file. There will always be a "M6T1" at the bottom of the text file, but it is not always the last line. It could be the 3rd line from the bottom, the 5th line from the bottom etc.

I only want to get rid of the last instance of "M6T1" so RegEx.Replace won't work here. I don't want to interfer with the other "M6T1"'s in the other locations in the text file.

Can someone please give me a solution to this problem?

Thanks

解决方案

var needle = "M6T1";
var ix = str.LastIndexOf(needle);
str = str.Substring(0, ix) + str.Substring(ix + needle.Length);

这篇关于删除从文本文件某些字符串的最后一个实例,而不改变字符串的其他实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆