lilyyan
asked on
question about replace string
Hello,
i got a question about string replace.
e.g. the string: Life health Science IS Good for HEALTH
first: i want to search the word: "health", case insensitive, so "health" and "HEALTH" will be found.
then i want to replace "health" with <b>health</b>; HEALTH with <b>HEALTH</b>
so the string is: Life <b>health</b> Science IS Good for <b>HEALTH</b>
my question is, in order to replace and find the word "health", the string will be changed into lower case,
how i can get: Life <b>health</b> Science IS Good for <b>HEALTH</b>
instead of: life <b>health</b> science is good for <b>health</b>
Thanks so much for your reply,
lilyyan
i got a question about string replace.
e.g. the string: Life health Science IS Good for HEALTH
first: i want to search the word: "health", case insensitive, so "health" and "HEALTH" will be found.
then i want to replace "health" with <b>health</b>; HEALTH with <b>HEALTH</b>
so the string is: Life <b>health</b> Science IS Good for <b>HEALTH</b>
my question is, in order to replace and find the word "health", the string will be changed into lower case,
how i can get: Life <b>health</b> Science IS Good for <b>HEALTH</b>
instead of: life <b>health</b> science is good for <b>health</b>
Thanks so much for your reply,
lilyyan
ASKER
the current code for replacement is like following :
========================== ========== ===
Sting testStr ="Life health Science IS Good for HEALTH";
String strPat ="HeaLth";
strPat =" " + strPat +" "; // add some leading and trailing space
testStr = " " + testStr + " ";
strReplace = " " + <b> + strPat.toLowerCase() +</b>;
if ( testStr.toLowercase().inde xOf (strPat.toLowerCase()) != -1 ){
testStr = highlighStr(testStr.toLowe rcase(), strPat.toLowercase(), strReplace)
}
/* -- find the string and replace it with highligt --*/
String highlighStr(String source, String pattern, String replace)
{
if (source!=null)
{
final int len = pattern.length();
StringBuffer sb = new StringBuffer();
int found = -1;
int start = 0;
while( (found = source.indexOf(pattern, start) ) != -1) {
sb.append(source.substring (start, found));
sb.append(replace);
start = found + len;
}
sb.append(source.substring (start));
return sb.toString();
}
else return "";
}
==========================
Sting testStr ="Life health Science IS Good for HEALTH";
String strPat ="HeaLth";
strPat =" " + strPat +" "; // add some leading and trailing space
testStr = " " + testStr + " ";
strReplace = " " + <b> + strPat.toLowerCase() +</b>;
if ( testStr.toLowercase().inde
testStr = highlighStr(testStr.toLowe
}
/* -- find the string and replace it with highligt --*/
String highlighStr(String source, String pattern, String replace)
{
if (source!=null)
{
final int len = pattern.length();
StringBuffer sb = new StringBuffer();
int found = -1;
int start = 0;
while( (found = source.indexOf(pattern, start) ) != -1) {
sb.append(source.substring
sb.append(replace);
start = found + len;
}
sb.append(source.substring
return sb.toString();
}
else return "";
}
ASKER
hi, i will get back to this post tomorrow. appreciate your reply.
Is this only for Upper case and Lower case issue only?
How if the string like
Life health Science IS Good for HealTH
Do you want it to be
Life <b>health</b> Science IS Good for <b>HEALTH</b>
or
Life <b>health</b> Science IS Good for <b>HealTH</b>
How if the string like
Life health Science IS Good for HealTH
Do you want it to be
Life <b>health</b> Science IS Good for <b>HEALTH</b>
or
Life <b>health</b> Science IS Good for <b>HealTH</b>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi, thanks for all replies. Well, I had to use the JDK 1.3, so I can't use replaceAll.
to suprapto45 : I agree with your suggestion that parameter "replace" is not necessary. i'm changing the code, will get back later.
to suprapto45 : I agree with your suggestion that parameter "replace" is not necessary. i'm changing the code, will get back later.
ASKER
hi, thanks for all sugestions , i already got the answer.
:-)
Glad I could help :)
David
David
Please post your current codes.