Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Case Insensative search

 
john klucas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
}

}

 
john klucas
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic