从两个字符串中删除重复项 [英] Removing duplicates from two strings

查看:84
本文介绍了从两个字符串中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Plz帮助我得到两个字符串值

string a =1,3,2,4,5;

b =3,4;

c = ab;

c =1,2,5;

从第一个字符串中删除第二个字符串值

和我希望结果为1,2,5

Plz Help I got two string values
string a= "1,3,2,4,5";
b = "3,4";
c = a-b;
c = "1,2,5";
remove second string value from first string
and I want the result as "1,2,5"

推荐答案

var s1 = "1,2,3,4,5";
var s2 = "3,4";
s1 = s1.Replace(s2, string.Empty).Replace(",,",",");





您在这里使用替换方法两次,第一次删除第二个字符串,第二个查找double,并将其替换为一个,



You are using the "Replace" method twice here, the first removes the second string, the second looks for double , and replaces it with a single ,


听起来像家庭作业。试试这个



Sounds like Homework. Try this

char[] delimiterChars = { ',' };

string a = "1,3,2,4,5";
string b = "3,4";

string[] A = a.Split(delimiterChars);
string[] B = b.Split(delimiterChars);

List<string> C = A.Except<string>(B).ToList();
string c = string.Join(",", C);


尝试以下代码



try below code

for (int i = 0; i < s1.Length; i++)
{
    int count = 0;
    if (s1[i] != ',')
    {

        for (int j = 0; j < s2.Length; j++)
        {
            if (s1[i].ToString() == s2[j].ToString())
            {
                count++;
            }
        }
        if (count == 0)
            s3 += s1[i].ToString() + ',';
    }
}
s3.Remove(s3.Length - 1);


这篇关于从两个字符串中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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