Solved

question about replace string

Posted on 2006-10-26
10
173 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

863 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

25 Experts available now in Live!

Get 1:1 Help Now