Solved

question about replace string

Posted on 2006-10-26
10
172 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
Comment Utility
Hi,

Please post your current codes.
0
 

Author Comment

by:lilyyan
Comment Utility
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
Comment Utility
hi, i will get back to this post tomorrow. appreciate your reply.
0
 
LVL 16

Expert Comment

by:suprapto45
Comment Utility
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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 86

Assisted Solution

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

Author Comment

by:lilyyan
Comment Utility
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
Comment Utility
hi, thanks for all sugestions , i already got the answer.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0
 
LVL 16

Expert Comment

by:suprapto45
Comment Utility
Glad I could help :)

David
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now