如何消除两个字符串中的重复char [英] How to eliminate the repeated char in two strings

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

问题描述

嗨专家..



我想消除两个字符串中重复的字符串。

例如如果我给

First string ==> vijayadhas

第二个字符串==> nasrya



输出应该是vijdha nr



First String ==> arun prasath

Second String ==> ramya



输出应为==> unrsath my



我试试这段代码。但它不是很完美



//字符串s1 =vijayadhas;

//字符串s2 =nasrya;

string s1 =arunprasath;

string s2 =ramya;

ArrayList name1 = new ArrayList();

ArrayList name2 = new ArrayList();



for(int i = 0; i< s1.Length; i ++)

{

name1.Add(s1 [i]);

}



for(int i = 0 ; i< s2.Length; i ++)

{

name2.Add(s2 [i]);

}



for(int i = 0; i< name1.Count; i ++)

{

for(int j = 0; j< name2.Count; j ++)

{

if(s1 [i] == s2 [j])

{

name1.R emoveAt(i);

name2.RemoveAt(j);

}

}

}



int len = name1.Count + name2.Count;

Console.WriteLine(剩余Chars的长度==>+ len) ;



Console.WriteLine(\ n \ n Name 1 ==>);

for(int i = 0;我< name1.Count; i ++)

{

Console.Write(name1 [i]);

}

Console.WriteLine( \ n \\ n名称2 ==>);

for(int i = 0; i< name2.Count; i ++)

{

Console.Write(name2 [i]);

}





这个程序的输出是==> rnpraah y



我必须改变以获得正确的输出?

请帮助我。

Hi Experts..

I want to eliminate the repeated strings in Two strings.
For Example If i give
First string ==> vijayadhas
Second string ==> nasrya

The out put should be vijdha nr

First String ==> arun prasath
Second String ==> ramya

The output should be ==> unprsath my

I try this code. But it is not working perfect

//string s1 = "vijayadhas";
//string s2 = "nasrya";
string s1 = "arunprasath";
string s2 = "ramya";
ArrayList name1 = new ArrayList();
ArrayList name2 = new ArrayList();

for (int i = 0; i < s1.Length; i++)
{
name1.Add(s1[i]);
}

for (int i = 0; i < s2.Length; i++)
{
name2.Add(s2[i]);
}

for (int i = 0; i < name1.Count; i++)
{
for (int j = 0; j < name2.Count; j++)
{
if (s1[i] == s2[j])
{
name1.RemoveAt(i);
name2.RemoveAt(j);
}
}
}

int len = name1.Count + name2.Count;
Console.WriteLine("Length of the remaing Chars==>"+len);

Console.WriteLine("\n\n Name 1==>");
for (int i = 0; i < name1.Count; i++)
{
Console.Write(name1[i]);
}
Console.WriteLine("\n\n Name 2==>");
for (int i = 0; i < name2.Count; i++)
{
Console.Write(name2[i]);
}


The out put for this program is ==> rnpraah y

Where i have to change to get the correct output?
Please help me.

推荐答案

string s1 = "arunprasath";
string s2 = "ramya";
string result;

for (int xIx = 0; xIx < s1.Length; xIx++)
{
    for (int xKx= 0; xKx < s2.Length; xKx++)
    {
        if (s1[xIx] == s2[xKx])
        {
            s1= s1.Remove(xIx--, 1);
            // One -- to mutch
            // s2= s2.Remove(xKx--, 1);
            s2= s2.Remove(xKIx, 1);
            break;
        }
    }
}
result = s1 + " " + s2;


我不确定为什么你认为你需要嵌套循环 - 所有你需要做的就是看看是否有字符串中的特定字符。

以下是我要做的事情:

I';m not sure why you think you need nested loops - all you need to do is look and see if there is a specific character in the string already.
Here is what I'd do:
private string RemoveThese(string input, string remove)
    {
    foreach (char c in remove)
        {
        int found = input.IndexOf(c);
        if (found >= 0) input = input.Remove(found, 1);
        }
    return input;
    }

它的作用是通过检查IndexOf(找到第一次匹配)是否有效来查找并删除remove字符串中的所有字符,如果是,则删除该字符。

所有你需要做的就是调用方法两次:

What it does is finds and removes all the characters in the remove string by checking if the IndexOf (which finds the first time it matches) is valid, and removing the character if it is.
All you then have to do is call the method twice:

string output = RemoveThese(name1, name2) + RemoveThese(name2, name1);





(事实上,这不是我做的方式 - 因为它非常低效因为字符串是不可变的,所以在左边和中间创建了新的字符串 - 但是因为我怀疑你还没有进入StringBuilder类,所以我不想让你感到困惑)



(As it happens, that isn't the way I'd do it - it is horribly inefficient since it creates new strings left right and centre, since strings are immutable - but since I suspect you haven't got to the StringBuilder class yet, I didn't want to confuse you)


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

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