Solved

Replace all in JDK 1.3

Posted on 2009-05-04
4
1,455 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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…

734 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