Solved

Comparing strings and ignoring the letters’ cases

Posted on 2006-07-19
24
287 Views
Last Modified: 2010-03-31
If I want to compare a string and considering all cases of letter

For example;

If(string.equals(“string”)|| string.equals(“String”)|| string.equals(“sTring”)|| string.equals(“STRING”)||...)

Is there a method to ignore the cases of letters and compare for a string letter only?
0
Comment
Question by:javamate06
  • 10
  • 5
  • 3
  • +3
24 Comments
 
LVL 10

Expert Comment

by:radarsh
ID: 17136476
Hi javamate06,

yes. use string.equalsIgnoreCase("string")
________
radarsh
0
 
LVL 35

Expert Comment

by:girionis
ID: 17136478
Use

string.compareToIgnoreCase(str)
0
 

Author Comment

by:javamate06
ID: 17136524
I knew, but I use it to compare the extension of file:

if((string.length()-3).equalsIgnoreCase(“.tt”))

does not work

but

if((string.length()-3) == (“.tt”))

it was OK.

So, how I can solve it.

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 35

Expert Comment

by:girionis
ID: 17136531
javamate06,
> if((string.length()-3) == (“.tt”))

Are you sure this will work? THis shouldn't compile at all.
0
 

Author Comment

by:javamate06
ID: 17136540
Sorry;

if((string.length()-3) == string.lastIndexOf(“.tt”))
0
 
LVL 35

Expert Comment

by:girionis
ID: 17136544
javamate06,
> if((string.length()-3).equalsIgnoreCase(“.tt”))

you should do

if((string.substring(0, string.length()-3)).equalsIgnoreCase(“.tt”))
0
 
LVL 35

Expert Comment

by:girionis
ID: 17136550
Hmm.. I guess you want to compare the file names, without the extensions...

if((string.substring(0, string.length()-3)).equalsIgnoreCase(<file name>))
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17136555
try this
int idx = string.lastIndexOf('.');
if(idx>0) {
    String ext = string.substring(0, idx);
    if(".tt".equalsIgnoreCase(ext)) {
    ...
    }
}
0
 
LVL 35

Expert Comment

by:girionis
ID: 17136557
or maybe (if you also want to get rid of the extension

if((string.substring(0, string.length()-3)).equalsIgnoreCase(filename.substring(0, filename.length()-3))
0
 

Author Comment

by:javamate06
ID: 17136647
All trials not working because I do not need to check the file name I want only the extension.

here my condition

   String str= "C:\\D\\j\\m\\t\\p\\test.abc";  
       if((str.substring(0,str.length()-4)).equalsIgnoreCase(".abc"))
        {
              System.out.println("OK");
        }
       else
       {
                 System.out.println("Not OK");
       }
    }
0
 
LVL 35

Expert Comment

by:girionis
ID: 17136679
try

iff((string.substring(string.length()-3, string.length())).equalsIgnoreCase(filename.substring(filename.length()-3, filename.length()))
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17136736
> int idx = string.lastIndexOf('.');
> if(idx>0) {
>     String ext = string.substring(0, idx);
>     if(".tt".equalsIgnoreCase(ext)) {
>     ...
>     }
> }

did you try this?
0
 

Author Comment

by:javamate06
ID: 17136739
In my method I do not have anything to do with file name


I have string that have the file path and
I want to check it if it have an extinction .abc or not
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17136747
> String ext = string.substring(0, idx);

sorry this should be

String ext = string.substring(idx);
0
 
LVL 35

Accepted Solution

by:
girionis earned 35 total points
ID: 17136754
My code above should work with minor modifications

if((string.substring(string.length()-3).equalsIgnoreCase("abc")
0
 
LVL 35

Expert Comment

by:girionis
ID: 17136756
the other way round

if(("abc".equalsIgnoreCase(string.substring(string.length()-3))
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17136786
if (string.matches("(?i).+\\.tt"))
0
 
LVL 92

Expert Comment

by:objects
ID: 17136794
if (s.substring(s.lastIndexOf('.')+1).equalsIgnoreCase("abc"))
0
 

Author Comment

by:javamate06
ID: 17137143
I accepted girionis

But, what does the intelligence method mean??

 if (string.matches("(?i).+\\.tt"))
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17137151
>>But, what does the intelligence method mean??

Firstly - have you tried it? ;-)
0
 
LVL 35

Expert Comment

by:girionis
ID: 17137174
javamate06,
>  if (string.matches("(?i).+\\.tt"))

It means, match the rest of the word (not the extension) but then give up the match and only keep the result, i.e. the extension.
0
 
LVL 92

Expert Comment

by:objects
ID: 17137182
and also makes it a lot slower :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17137201
>>and only keep the result

Actually nothing is kept. String should also have the convenience method

public int find(String re)

and then one could do

if (string.find("(?i)\\.tt$") > -1)
0
 
LVL 35

Expert Comment

by:girionis
ID: 17137208
javamate06, thank you for accepting, glad the problem is solved :)
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 117
javap not working 8 46
Way to decrease size of apk file 9 72
Detect Closed Loops (circles, figure-8s, etc) in PNG Images 6 34
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question