Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

question about replace string

Posted on 2006-10-26
10
Medium Priority
?
179 Views
Last Modified: 2010-03-31
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

0
Comment
Question by:lilyyan
  • 4
  • 4
  • 2
10 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 17816669
Hi,

Please post your current codes.
0
 

Author Comment

by:lilyyan
ID: 17816728
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 "";
    }

0
 

Author Comment

by:lilyyan
ID: 17816742
hi, i will get back to this post tomorrow. appreciate your reply.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Expert Comment

by:suprapto45
ID: 17816769
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>
0
 
LVL 16

Accepted Solution

by:
suprapto45 earned 1600 total points
ID: 17816791
If the you want it to be the second issue,

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.toLowerCase().indexOf(pattern.toLowerCase(), start) ) != -1) {
            sb.append(source.substring(start, found));
          
          sb.append("<b>" + source.substring(found, found + pattern.length()) + "</b>");
            start = found + len;
        }

        sb.append(source.substring(start));

        return sb.toString();
        }
        else return "";
    }
}

I do think that you do not need the "replace" parameter anymore and this function is only useful if you want to add the <b></b>. For other tags, you can't. Hence, you have some options whether you want to stick with the old codes but you have to call it twice such as to check "health" and "HEALTH" manually. Or using this new codes but only work for <b></b>
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 17817702
String s = "Life health Science IS Good for HEALTH".replaceAll("(?i)(health)", "<b>$1</b>");
0
 

Author Comment

by:lilyyan
ID: 17818759
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.
0
 

Author Comment

by:lilyyan
ID: 17822015
hi, thanks for all sugestions , i already got the answer.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17824705
:-)
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17825459
Glad I could help :)

David
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question