Solved

Replace all in JDK 1.3

Posted on 2009-05-04
4
1,422 Views
Last Modified: 2012-05-06
Hey experts,

Our production jdk is 1.3 and i need to replace string "ignore case"
This is the mehod i used,it replaces all string  but "case sensitive", what should l modify ?

public static String replaceAll(String source, String toReplace, String replacement) {
 

		// this code is written by David Hilsee

		int idx = source.lastIndexOf( toReplace );

		if ( idx != -1 ) {

			StringBuffer ret = new StringBuffer( source );

			ret.replace( idx, idx+toReplace.length(), replacement );

			while( (idx=source.lastIndexOf(toReplace, idx-1)) != -1 ) {

				ret.replace( idx, idx+toReplace.length(), replacement );

			}

			source = ret.toString();

		}

		return source;

	}

Open in new window

0
Comment
Question by:Web_Admin
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 24301892
try this:

public static String replaceAll(String source, String toReplace, String replacement) {
                String lower = source.toLowerCase();
                String lowerReplace = toReplace.toLowerCase();
                // this code is written by David Hilsee
                int idx = lower.lastIndexOf( lowerReplace );
                if ( idx != -1 ) {
                        StringBuffer ret = new StringBuffer( source );
                        ret.replace( idx, idx+toReplace.length(), replacement );
                        while( (idx=lower.lastIndexOf(lowerReplace, idx-1)) != -1 ) {
                                ret.replace( idx, idx+toReplace.length(), replacement );
                        }
                        source = ret.toString();
                }
                return source;
        }
0
 

Author Comment

by:Web_Admin
ID: 24311833
Hi objects,

ur method return the source all lower case letters and i don't want that, i need the method search the source string, find the match toReplace and do the replacement not to convert all source to lower case

for example

source: Google
to Replace: g
replacement: <font>g or G</font>
find letter g "capital or small letter" -- don't change it to small letter
final output String <font>G</font>oo<font>g</font>le

hope i make it clear
0
 
LVL 92

Expert Comment

by:objects
ID: 24311890
It doesn't convert it to all lower case, it just does case insensitive comparison

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 customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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.

930 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

10 Experts available now in Live!

Get 1:1 Help Now