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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# How to compare each individual char of 2 string

Greenhorn
Posts: 9
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:
It must be case insensitive.

Phal Ach
Ranch Hand
Posts: 54
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 13082
67
• Number of slices to send:
Optional 'thank-you' note:
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 ]

 knowledge is the difference between drudgery and strategic action -- tiny ad the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising