C#:如何删除2个字符串之间的匹配子字符串? [英] C#: How to Delete the matching substring between 2 strings?

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

问题描述

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

If I have two strings .. say

string1 ="Hello Dear c'Lint"

string1="Hello Dear c'Lint"

string2 =亲爱的"

string2="Dear"

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

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

你好,c'Lint"

"Hello  c'Lint"

(即,"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个字符串之间的匹配子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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