akash89
akash89

Reputation: 891

Comparing two strings by character in java

I have 2 strings :

Requirement is , O/P should return me the string with only those characters in first but not in second. Eg. in this case O/P is SL

Eg2.

, o/p = "ASD"

For this, the coding I have developed:

String one = "pnlm";
String two ="bsnl";
String fin = "";

for(int i =0; i<one.length();i++)
        {
            for(int j=0;j<two.length();j++)
            {

                //System.out.print(" "+two.charAt(j));

                if(one.charAt(i) == two.charAt(j))
                {
                    fin+=one.charAt(i);

                }

            }

        }

ch=removeDuplicates(fin);
        System.out.print(" Ret ::"+fin);

        System.out.println("\n Val ::"+ch);

CH gives me the string with equal characters, but using this logic i cant get the unequal characters.

Can anyone please help?

Upvotes: 0

Views: 6117

Answers (4)

Ganesh
Ganesh

Reputation: 482

I have simple exchange your logic, see:

String one = "pnlm";
String two = "bsnl";
String fin = "";
int cnt;
for (int i = 0; i < one.length(); i++) {
    cnt = 0; // zero for no character equal
    for (int j = 0; j < two.length(); j++) {
        //  System.out.print(" "+two.charAt(j));

        if (one.charAt(i) == two.charAt(j)) {
            cnt = 1; // ont for character equal
        }

    }
    if (cnt == 0) {
        fin += one.charAt(i);
    }
}

System.out.print(" Ret ::" + fin);

o/p: Ret ::pm.

Upvotes: 2

TofuBeer
TofuBeer

Reputation: 61534

public static void main(String[] args) 
{
    String one = "ASDR";
    String two ="MRT";
    String fin = unique(one, two);

    System.out.println(fin);
}

private static String unique(final String one,
                             final String two)
{
    final List<Character> base;
    final Set<Character>  toRemove;
    final StringBuilder   remaining;

    base = new ArrayList<>(one.length());
    toRemove = new HashSet<>();

    for(final char c : one.toCharArray())
    {
        base.add(c);
    }

    for(final char c : two.toCharArray())
    {
        toRemove.add(c);
    }

    base.removeAll(toRemove);
    remaining = new StringBuilder(base.size());

    for(final char c : base)
    {
        remaining.append(c);
    }

    return (remaining.toString());
}

Upvotes: 0

Gabriel Negut
Gabriel Negut

Reputation: 13960

  1. Iterate over the first string
  2. For each character, check if the second string contains it
  3. If it doesn't, add the caracter to a StringBuilder
  4. Return stringBuilder.toString()

Upvotes: -1

Rod_Algonquin
Rod_Algonquin

Reputation: 26198

You can use the Set interface to add all the second array of character so you can check it there later.

sample:

String one = "ASDR";
String two ="MRT";
StringBuilder s = new StringBuilder();

Set<Character> set = new HashSet<>();
for(char c : two.toCharArray())
    set.add(c); //add all second string character to set
for(char c : one.toCharArray())
{
    if(!set.contains(c)) //check if the character is not one of the character of second string
        s.append(c); //append the current character to the pool
}

System.out.println(s);

result:

ASD

Upvotes: 3

Related Questions