Not finding text because it is case sensitive

I am having problem with my highlights method. I want to search a text area with the pattern entered in the text field. At the moment, it is case sensitive and correctly find the pattern if the case are the same. How can I make it find the pattern when the case are different?

For example:

Text Area:- I love PrograMMing in Java

Pattern to find:- programming

It does not successfully find programming because programming in text area is written as: PrograMMing.

// Creates highlights around all occurrences of pattern in log

publicvoid highlight(JTextComponent textComp, String pattern){

try{

Highlighter hilite = textComp.getHighlighter();

Document doc = textComp.getDocument();

String text = doc.getText(0, doc.getLength());

int pos = 0;

boolean foundAnyText =false;

// Search for pattern

while ((pos = text.indexOf(pattern, pos)) >= 0){

// Create highlighter using private painter and apply around pattern

hilite.addHighlight( pos, pos + pattern.length(), myHighlightPainter );

pos += pattern.length();

foundAnyText =true;

}

if(!foundAnyText){

JOptionPane.showMessageDialog( null,"Text not found." );

}

}catch (BadLocationException e){

System.out.println("BadLocationException: " + e.getMessage() );

}

}

[2115 byte] By [SDNJavaa] at [2007-11-27 11:58:50]
# 1

text.toLowerCase().indexOf(sample.toLowerCase());

CeciNEstPasUnProgrammeura at 2007-7-29 19:22:08 > top of Java-index,Java Essentials,Java Programming...
# 2

Thank you CeciNEstPasUnProgrammeur.

SDNJavaa at 2007-7-29 19:22:08 > top of Java-index,Java Essentials,Java Programming...
# 3

There is a problem with this solution.

while ((pos = text.toLowerCase().indexOf(pattern.toLowerCase(), pos)) >= 0) {

// Create highlighter using private painter and apply around pattern

hilite.addHighlight( pos, pos + pattern.length(), myHighlightPainter );

pos += pattern.length();

foundAnyText = true;

}

In my text area, I have a lot of XML tags e.g.:

<_01_2:Name>Tom</_01_2: Name>

<_01_2:Address>12 Tom Road</_01_2:Address>

<_01_2:Sport>Football</_01_2: Sport >

<_01_2:Job>Programmer</_01_2:Job>

When the user enters any of the pattern <,_,0,1,2,:/ the CPU usage of my computer goes to 100% for about 60 seconds before it highlight the text. This is because it trying to convert any of the pattern <,_,0,1,2,:/ into lowercase and can not handle it. This is an interesting problem. Any idea how to overcome this problem.

SDNJavaa at 2007-7-29 19:22:08 > top of Java-index,Java Essentials,Java Programming...
# 4

Try it this way:

String text0 = text.toLowerCase();

String pattern0 = pattern.toLowerCase();

int len = pattern.length();

int pos = 0; // I assume you're already doing this

while ((pos = text0.indexOf(pattern0, pos)) >= 0) {

// Create highlighter using private painter and apply around pattern

hilite.addHighlight( pos, pos + len, myHighlightPainter );

pos += len;

foundAnyText = true;

}

uncle_alicea at 2007-7-29 19:22:08 > top of Java-index,Java Essentials,Java Programming...
# 5

Thank you. Converting the text and the pattern in the while loop creates lots of processes. By doing it before the while loop avoids this problem.

SDNJavaa at 2007-7-29 19:22:08 > top of Java-index,Java Essentials,Java Programming...