?
Solved

Comparing strings and ignoring the letters’ cases

Posted on 2006-07-19
24
Medium Priority
?
291 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 140 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 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