Solved

question about replace string

Posted on 2006-10-26
10
174 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 195
Java JRE greater than 1.6 5 63
table example 4 29
pagenation logic how it is working in my code 1 30
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

809 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