删除2个单词之间的字符串 [英] Delete strings between 2 words
问题描述
大家好
以下是我的字符串
Hi all
Below is my string
name = root_fs_3
volume = root_volume_3
name = root_fs_vdm_vdm_srv1
volume = v400
volume: total = 128 (sizes in MB) ( blockcount = 262144 ) avail = 104 used = 24 ( 19% )
name = Shared
volume = v402
volume: total = 4718592(sizes in MB)(blockcount=9663676416) avail=1631438 used=3087154(65% )
在此基于以下条件我想删除字符串
如果字符串包含名称和root然后删除所有字符串,直到我们再次获得名称。
请帮我如何删除2个相同单词之间的字符串。
In this based on below condition i want to delete the strings
if string contains "name" and "root" then delete all the strings until we are getting "name" again.
Please help me how to delete strings between 2 same words.
推荐答案
我假设您正在讨论单独考虑的每一行,因此name和root必须存在于同一行。如果没有那么
I am assuming that you are talking about each line being considered separately, so "name" and "root" must exist on the same line. If not then
name = shared
volume = rooterBackup
会触发删除周期。
因此,将输入处理为一个行数组,每行包含一个新字符串。这是微不足道的:String.Split会这样做。
然后循环,带有删除标志和输出的新集合 - 这可能是List< string>,或者StringBuilder取决于什么您的代码将随后执行。
如果该行以name开头,则删除将被更改。如果包含root,则删除为true,否则为false。
如果不删除,请将该行添加到输出集合中。如果没有,请不要。
Would trigger a delete cycle.
So process your input into an array of lines, each line in a new string. That's trivial: String.Split will do that.
Then loop, with a "deleting flag" and a new collection for the output - this could be a List<string>, or a StringBuilder depending on what your code is going to do subsequently.
If the line starts with "name" then deleting is changed. Is if contains "root", deleting is true, otherwise it's false.
If you are not deleting, add the line to the output collection. If not, don't.
这篇关于删除2个单词之间的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!