14 Replies Latest reply: Jun 12, 2008 12:19 PM by 807591

# Compare individual char of 2 string

Hi

I need to compare the each individual char of 2 string.

String a = "Ae12";
String b = "aE12";

// I tried the followings:
// a
String c = a.substring(0,0);
String d = a.substring(1,1);
String e = a.substring(2,2);
String f = a.substring(3,3);
// b
String c1 = b.substring(0,0);
String d1 = b.substring(1,1);
String e1 = b.substring(2,2);
String f1 = b.substring(3,3);

if(c.equalsIgnoreCase(c1)&&d.equalsIgnoreCase(d1)&&e.equalsIgnoreCase(e1)&&f.equalsIgnoreCase(f1)) // Cannot be compared

// and I tried the followings:
// a
char c = a.charAt(0);
char d = a.charAt(1);
char e = a.charAt(2);
char f = a.charAt(3);
// b
char c1 = b.charAt(0);
char d1 = b.charAt(1);
char e1 = b.charAt(2);
char f1 = b.charAt(3);

if(c==c1&&d==d1&&e==e1&&f==f1) // cannot be compared.

Both cases cannot be compared. How can I fix it?

Because I want to compare those 2 strings without case sensitive and numbers.

Thanks
• ###### 1. Re: Compare individual char of 2 string
What do you mean by "cannot be compared"? Both of those methods look fine.
• ###### 2. Re: Compare individual char of 2 string
Both return false in if statement.
• ###### 3. Re: Compare individual char of 2 string
What do you mean "cannot be compared"? Of course they can, that can't be the error you're getting, so what is?

And, if you need to compare every character, why don't you just compare the entire String?

Edit: Man, I'm slow.
• ###### 4. Re: Compare individual char of 2 string
techissue2008 wrote:
String c = a.substring(0,0);
String d = a.substring(1,1);
String e = a.substring(2,2);
String f = a.substring(3,3);
FYI, these are all empty strings...
• ###### 5. Re: Compare individual char of 2 string
Well, the second one will have to return false, since case is not ignored there, for the first here is a quote from the API docs:
Returns a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

i.e. substring(3,3) will, of course, return "", you want substring(3,4)

Edit: Man, and I am still far too slow.

But, again, why don't you just compare the entire String with equalsIgnoreCase, if you compare every character anyway?
• ###### 6. Re: Compare individual char of 2 string
If I compare 2 string, a.equalsIgnoreCase(b), it returns false. Is it due to there are numbers in the string?
• ###### 7. Re: Compare individual char of 2 string
techissue2008 wrote:
If I compare 2 string, a.equalsIgnoreCase(b), it returns false. Is it due to there are numbers in the string?
That I would need to see first. Are you sure the Strings are actually the same? I.E. no spaces, special characters, etc.
• ###### 8. Re: Compare individual char of 2 string
techissue2008 wrote:
If I compare 2 string, a.equalsIgnoreCase(b), it returns false. Is it due to there are numbers in the string?
No. It's because the two strings are not the same, even when you ignore upper and lower case. How about posting a complete example of this instead of just spreading rumours?
• ###### 9. Re: Compare individual char of 2 string
I have to compare 2 string which came from the combination of 4 letters of "0123456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ"

So, they must no special char and no space.

Besides numbers, the letter must be compared with case insensitive.
• ###### 10. Re: Compare individual char of 2 string
Okay. And? equalsIgnoreCase still will not return false when the Strings are the same. So, the question remains, are you sure the Strings are actually the same? Post your real code.
• ###### 11. Re: Compare individual char of 2 string
String a = "Ae12";
String b = "aE12";

Then, are "Ae12" and "aE12" the same when ignore case?

If so, the Strings are actually the same.
• ###### 12. Re: Compare individual char of 2 string
techissue2008 wrote:
String a = "Ae12";
String b = "aE12";

Then, are "Ae12" and "aE12" the same when ignore case?

If so, the Strings are actually the same.
No, the Strings are not the same, if by same, you mean the "same" Object, or the "same" value when case is not ignored, but they are the "same" value when case is ignored.

This prints true (try it yourself), as it should
``````public class Test {
public static void main (String[] args) {
System.out.println("aE12".equalsIgnoreCase("Ae12"));
}
}``````
They are still two distinct instances with two distinct values however, so where, exactly, is your problem?
• ###### 13. Re: Compare individual char of 2 string
I finally use:
String c = a.substring(0,1);
String d = a.substring(1,2);
String e = a.substring(2,3);
String f = a.substring(3,4);

String c1 = b.substring(0,1);
String d1 = b.substring(1,2);
String e1 = b.substring(2,3);
String f1 = b.substring(3,4);

They can show letter for those string and not be null. However, if I use

if(!c.equalsIgnoreCase(c1) || !d.equalsIgnoreCase(d1) || !e.equalsIgnoreCase(e1) || !f.equalsIgnoreCase(f1))
{
return error msg;
}
else{
return success;
}

Even both strings are the same, it still returns error msg.
• ###### 14. Re: Compare individual char of 2 string
This
``````if (!c.equalsIgnoreCase(c1) || !d.equalsIgnoreCase(d1) || !e.equalsIgnoreCase(e1) || !f.equalsIgnoreCase(f1)) {
return error msg;
} else {
return success;
}``````
will not even compile. Use code tags, and post at least functional code.

Edit: Nevermind.