假设我有两个字符串:
string s1 = "hello";
string s2 = "hello world";
有没有办法获取一个string s3 = " world";
,该字符串是两个字符串之间的差异?
编辑:
在这种情况下,差异将始终存在。s1 = "abc"
s2 = "abcd ads as "
使用string s3 = s2.Replace(s1, "");
编辑:请注意,s2
中所有的s1
出现的位置都将从s3
中删除。确保仔细考虑本帖子中的评论,以确认这是您所需的结果,例如@mellamokb的评论中提到的情况。
string s1 = "hello";
string s2 = "hello world";
string s3 = s2.replace(s1,"");
string s3 = s2.substring(s1.Length);
通过简单的替换
string s3 = s2.Replace(s1, "");
在代码之外,第一个没有条件的答案:
string s3 = null;
if (s2.StartsWith(s1))
{
s3 = s2.Substring(s1.Length);
}
s1 =“hello!”
和s2 =“hello world!”
的情况呢?或者是s1 =“bob the builder”
和s2 =“the bob”
的情况呢?是否总是只包含另一个字符串的精确子字符串? - mellamokb