Solved

Replace all in JDK 1.3

Posted on 2009-05-04
4
1,417 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
Comment Utility
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
Comment Utility
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
Comment Utility
It doesn't convert it to all lower case, it just does case insensitive comparison

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
java set up 1 45
eclipse compiler vs Installed JREs option 3 36
Securing Jmx Console and web console 2 45
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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

772 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

11 Experts available now in Live!

Get 1:1 Help Now