Solved

Replace all in JDK 1.3

Posted on 2009-05-04
4
1,428 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video teaches viewers about errors in exception handling.

786 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