wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Case Insensative search Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Case Insensative search" Watch "Case Insensative search" New topic
Author

Case Insensative search

john klucas
Greenhorn

Joined: Jul 31, 2001
Posts: 23
i want to have a case insensative search...can anyone help me out.....
this is the code wich find only the word entered..(all caps or small or finds the word as present there)
private void doFind() {
String temp4 = "";
temp4 = ivjTextField1.getText();
String s3 = ivjTextArea1.getText();
int total = s3.length();

if(isfound ==false)
{
int ix = s3.indexOf(temp4);
if(ix >= 0)
{
lastindex = ix+temp4.length();
ivjTextArea1.select(ix, lastindex);
isfound = true;
oldString = ivjTextField1.getText();
}
} else
{
int ix2 = s3.indexOf(temp4, lastindex+1);
if(ix2>=0)
{
lastindex = ix2+temp4.length();
ivjTextArea1.select(ix2, lastindex);
// oldString = ivjTextField1.getText();
}
}

if(!temp4.equals(oldString)){
isfound = false;
doFind();
}

}
Karen Parker
Greenhorn

Joined: May 16, 2001
Posts: 15
To make your search "case insensitive", you could convert both the search string and the text you are searching to all upper or lower case and then compare them.
For example:

would yield a new string with value: "thisisastring"
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
Why not just use String.equalsIgnoreCase(String)?
Originally posted by john klucas:
i want to have a case insensative search...can anyone help me out.....
this is the code wich find only the word entered..(all caps or small or finds the word as present there)
private void doFind() {
String temp4 = "";
temp4 = ivjTextField1.getText();
String s3 = ivjTextArea1.getText();
int total = s3.length();

if(isfound ==false)
{
int ix = s3.indexOf(temp4);
if(ix >= 0)
{
lastindex = ix+temp4.length();
ivjTextArea1.select(ix, lastindex);
isfound = true;
oldString = ivjTextField1.getText();
}
} else
{
int ix2 = s3.indexOf(temp4, lastindex+1);
if(ix2>=0)
{
lastindex = ix2+temp4.length();
ivjTextArea1.select(ix2, lastindex);
// oldString = ivjTextField1.getText();
}
}

if(!temp4.equals(oldString)){
isfound = false;
doFind();
}

}


There's nothing more dangerous than a resourceful idiot. ---Dilbert
john klucas
Greenhorn

Joined: Jul 31, 2001
Posts: 23
If u have lok at the code, i am not comparing two strings.
I have a text area where i am displaying the o/p.
so when the user wants to find anything..he just inputs that word...
and my serach function finds the index of that word and matches that in the text area.
its working fine if the users tries to find for a word which is exactly the same as it is present in the text area. So how can u do that??
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
String temp4 = "";
temp4 = ivjTextField1.getText();
String s3 = ivjTextArea1.getText();
int total = s3.length();

Explain how that is not comparing 2 strings. You do a getText() from 2 different components returning Strings. Like a previous poster said convert them to all upper or lower then do your search.
temp4 = ivjTextField1.getText().toUpperCase();
String s3 = ivjTextArea1.getText().toUpperCase();
john klucas
Greenhorn

Joined: Jul 31, 2001
Posts: 23
thanx
 
GeeCON Prague 2014
 
subject: Case Insensative search