programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Liutauras Vilda
• Campbell Ritchie
• Tim Cooke
• Bear Bibeault
• Devaka Cooray
Sheriffs:
• Jeanne Boyarsky
• Knute Snortum
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Ganesh Patekar
• Stephan van Hulst
• Pete Letkeman
• Carey Brown
Bartenders:
• Tim Holloway
• Ron McLeod
• Vijitha Kumara

# How to compare each individual char of 2 string

Greenhorn
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

Ranch Hand
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
Posts: 9
It must be case insensitive.

Phal Ach
Ranch Hand
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.

lowercase baba
Bartender
Posts: 12627
50
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 ]

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.