本文介绍了C#:如何删除2个字符串之间的匹配子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个字符串..说

If I have two strings ..say

..我要先比较字符串,然后删除匹配的子字符串..
上述字符串对的结果是:

.. I want to Compare the strings first and delete the matching substring ..
the result of the above string pairs is:

(即,"Hello" "c'Lint" 之间的两个空格)


为简单起见,我们'将假设string2将是string1的子集.(我是说string1将包含string2)..

(i.e, two spaces between "Hello" and "c'Lint")

for simplicity, we'll assume that string2 will be the sub-set of string1 .. (i mean string1 will contain string2)..

推荐答案

仅执行以下操作:

string string1 = textBox1.Text;
string string2 = textBox2.Text;

string string1_part1=string1.Substring(0, string1.IndexOf(string2));
string string1_part2=string1.Substring(
    string1.IndexOf(string2)+string2.Length, string1.Length - (string1.IndexOf(string2)+string2.Length));

string1 = string1_part1 + string1_part2;

希望它会有所帮助.它将仅删除首次出现的内容.

Hope it helps. It will remove only first occurance.

这篇关于C#:如何删除2个字符串之间的匹配子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 02:00