Solved

Comparing strings and ignoring the letters’ cases

Posted on 2006-07-19
24
280 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now