Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace all in JDK 1.3

Posted on 2009-05-04
4
Medium Priority
?
1,481 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
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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:
Suggested Courses

782 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