Search for a matching String

String numbers = "one two three four five six seven";

String searchWord = "One THREE";

basically what i want to do is create a method which gets the 'searchWord'

and look for matching words in the 'numbers' string.

for this i want to know

1- how to loop through a String to get substrings,avoinding spaces,commas,etc

2 - ignore case sensivity in the 'searchWord'

3 - highlight words 'one' & 'three' in the 'numbers' String

Please help!!.sample code would be nice

[549 byte] By [kuhasaa] at [2007-10-3 5:23:11]
# 1

Hi

This sounds pretty much like homework. Read the javadoc for the String class.

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html

Do especially read about substring, trim, and split.

Number 3 can't be solved using the String class. A string does not contain information about colors, fonts etc. Highlightning is something you do during presentation.

Kaj

kajbja at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 2
There are also methods to compare Strings and to handle case.
floundera at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 3

If, as I suspect, this is homework then the following will probably not be acceptable to the teacher but ...

String numbers = "one two three four One five six seven";

String searchWord = "One THREE";

Pattern p = Pattern.compile("(?i)(" + searchWord.replaceAll(" +","|") + ")");

Matcher m = p.matcher(numbers);

while (m.find())

{

System.out.println(m.group(1));

}

sabre150a at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 4
thanks for your help.It is greatly appreciated!!!!
kuhasaa at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 5
I have used sabre150 s code. But following case doesnt workI've got a String like -$6,963.55When I type 6963 the above string should be highlighted.But it is not!pls help.btw can you also explain whats "?i " in Pattern.compile("(?i)
kuhasaa at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 6

Ohhhh, so it's spelled "Luxury Yacht", but it's pronounced "Throat-wobbler Mangrove". :P

You need to give use a more detailed explanation of what you're trying to do, with several examples of inputs and desired results.

(?i) is an embedded flag that tells the regex to ignore case. You'll probably find this site helpful: http://www.regular-expressions.info/

uncle_alicea at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 7

ok.here's more code..

some of the code in jsp

WordHighlight wh = new WordHighlight();

<td align=right><%="$"+wh.getWordString(df1.format(cp.getGSTcost()),searchWord)%></td>

***********************************************

i am passing '6963' as the 'searchword' and the 'text' is 6,963.55

public class WordHighlight {

public String getWordString(String text,String searchWord) {

String startTag = "<span style=\"background-color: #00FFFF\">";

String endTag = "</span>";

int count = 0;

if (!searchWord.equals("")){

Pattern p = Pattern.compile("(?i)(" + searchWord.replaceAll(" +","|") + ")");

Matcher m = p.matcher(text);

while (m.find()) {

StringBuffer hResult = new StringBuffer (startTag);

hResult.append(m.group(1));

hResult.append(endTag);

text = text.replace(m.group(1), hResult.toString());

}

return text;

} else

return text;

}

}

hope this is clear enough for uncle :)

kuhasaa at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 8

> hope this is clear enough for uncle :)

It's probably not. Take a look on his post. This is what he said

You need to give use a more detailed explanation of what you're trying to do,

with several examples of inputs and desired results.

You see, we need to see the different types of valid input, and what you expect as output. The code which you have posted does not show what you consider as valid input.

Kaj

kajbja at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 9

Ooops..ok well

what i want to do is in my jsp page i've got a text box which i can type

values that i want to search .

say i have following values in my db table

3500

6250

on my report i am getting these values and formatting like $3,500.00 , $6,250.00

So when i type in 3500 (searchword)in the text box ,$ 3,500.00 in the report(output)

should get highlighted,but its not doing that.

So the code in my previous reply I am getting the 3500 as the search word and the text is '3,500.00' .If the search word and the text get matched I simply want to highlight it and pass the highlighted text

if u need any more infor.letmme know

thx!

kuhasaa at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...
# 10

If you assume that the search expression will never contain anything but digits, it isn't too difficult: String amountRegex = amount.replaceAll(("\\d{1,3}(?=(?:\\d{3})+$)", "$0,") + "\\.\\d\\d";

But it looks like you're allowing the user to search for multiple amounts, separated by commas. In that case, you need to split the search expression into individual amounts, do the replacement above on each one, then rejoin them, interspersed with pipes. Let us know if you have any problems with that. :)

BTW, I suggest you change your initial test on the search expression from if (!searchWord.equals(""))

to something like if (searchWord.matches("\\d+(?:\\s*,\\s*\\d+)*"))

Oh, and you don't need the (?i) modifier for this task.

uncle_alicea at 2007-7-14 23:30:16 > top of Java-index,Java Essentials,Java Programming...