aspose file tools
The moose likes Beginning Java and the fly likes How to compare each individual char of 2 string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to compare each individual char of 2 string" Watch "How to compare each individual char of 2 string" New topic
Author

How to compare each individual char of 2 string

Tommy Leung
Greenhorn

Joined: Jun 07, 2008
Posts: 9
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
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Try for loop it works. But it would be case sensitive. And it is always a bad idea to Store each and every characters in different variables and use lots of memory. Store them in variable on the fly in for loop. Compare it with charAt(index).
Tommy Leung
Greenhorn

Joined: Jun 07, 2008
Posts: 9
It must be case insensitive.
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
It can be case insensitive too. The you have to use substring(begin index, end index) along with for loop. Your code will be done in 8-9 lines.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

why do you think things cannot be compared this way? what exactly is wrong with this code?

granted it's not pretty, but why doesn't it work??? i took this:


and got this output:

C:\slop>java Slop
same


it looks like it worked to me...
[ June 12, 2008: Message edited by: fred rosenberger ]

Never ascribe to malice that which can be adequately explained by stupidity.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to compare each individual char of 2 string
 
Similar Threads
Copying Integer to a file
Remove Duplicate Rows
how to edit in apache hssf excel api
"==" Question from Kathy's Book
Magic Squares