Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 294
  • Last Modified:

Comparing strings and ignoring the letters’ cases

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
javamate06
Asked:
javamate06
  • 10
  • 5
  • 3
  • +3
1 Solution
 
radarshCommented:
Hi javamate06,

yes. use string.equalsIgnoreCase("string")
________
radarsh
0
 
girionisCommented:
Use

string.compareToIgnoreCase(str)
0
 
javamate06Author Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
girionisCommented:
javamate06,
> if((string.length()-3) == (“.tt”))

Are you sure this will work? THis shouldn't compile at all.
0
 
javamate06Author Commented:
Sorry;

if((string.length()-3) == string.lastIndexOf(“.tt”))
0
 
girionisCommented:
javamate06,
> if((string.length()-3).equalsIgnoreCase(“.tt”))

you should do

if((string.substring(0, string.length()-3)).equalsIgnoreCase(“.tt”))
0
 
girionisCommented:
Hmm.. I guess you want to compare the file names, without the extensions...

if((string.substring(0, string.length()-3)).equalsIgnoreCase(<file name>))
0
 
Ajay-SinghCommented:
try this
int idx = string.lastIndexOf('.');
if(idx>0) {
    String ext = string.substring(0, idx);
    if(".tt".equalsIgnoreCase(ext)) {
    ...
    }
}
0
 
girionisCommented:
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
 
javamate06Author Commented:
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
 
girionisCommented:
try

iff((string.substring(string.length()-3, string.length())).equalsIgnoreCase(filename.substring(filename.length()-3, filename.length()))
0
 
Ajay-SinghCommented:
> int idx = string.lastIndexOf('.');
> if(idx>0) {
>     String ext = string.substring(0, idx);
>     if(".tt".equalsIgnoreCase(ext)) {
>     ...
>     }
> }

did you try this?
0
 
javamate06Author Commented:
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
 
Ajay-SinghCommented:
> String ext = string.substring(0, idx);

sorry this should be

String ext = string.substring(idx);
0
 
girionisCommented:
My code above should work with minor modifications

if((string.substring(string.length()-3).equalsIgnoreCase("abc")
0
 
girionisCommented:
the other way round

if(("abc".equalsIgnoreCase(string.substring(string.length()-3))
0
 
CEHJCommented:
if (string.matches("(?i).+\\.tt"))
0
 
objectsCommented:
if (s.substring(s.lastIndexOf('.')+1).equalsIgnoreCase("abc"))
0
 
javamate06Author Commented:
I accepted girionis

But, what does the intelligence method mean??

 if (string.matches("(?i).+\\.tt"))
0
 
CEHJCommented:
>>But, what does the intelligence method mean??

Firstly - have you tried it? ;-)
0
 
girionisCommented:
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
 
objectsCommented:
and also makes it a lot slower :)
0
 
CEHJCommented:
>>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
 
girionisCommented:
javamate06, thank you for accepting, glad the problem is solved :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 10
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now