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
Solved

Comparing strings and ignoring the letters’ cases

Posted on 2006-07-19
24
288 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 119
numbers ascending pyramid 101 212
servlet example 11 49
Is there a simpler dropbox system? 10 34
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

860 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