Solved

Comparing strings and ignoring the letters’ cases

Posted on 2006-07-19
24
289 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 306
servlet example 17 59
Java string replace 11 64
jboss wildfly 10.1 10 437
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 how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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.

730 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