Solved

question about replace string

Posted on 2006-10-26
10
177 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 400 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 100 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

630 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