Link to home
Start Free TrialLog in
Avatar of lilyyan
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

Avatar of suprapto45
suprapto45
Flag of Singapore image

Hi,

Please post your current codes.
Avatar of lilyyan
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().indexOf (strPat.toLowerCase()) != -1 ){

testStr = highlighStr(testStr.toLowercase(), 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 "";
    }

Avatar of lilyyan

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>
ASKER CERTIFIED SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lilyyan

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.
Avatar of lilyyan

ASKER

hi, thanks for all sugestions , i already got the answer.
:-)
Glad I could help :)

David