Replacing strings with Java and Javascript

Java and Javascript have different ways to replace strings like the feature “find and replace” that you see in text editors.

Java’s replace method is not ideal for noobs but only for people who know about regex. I am not that quite familiar with regex and if you wish to use that replace method under the String class in Java, you’d have to familiarize yourself with it in order to use it. The String class though, has lots of other useful methods that can be used to simulate how a search and replace function works. Here is a workaround method for people who only want a simple search and replace with the search and target both String. No regex knowledge needed.


public String replaceString(String strOrig, String strFind, String strReplace) {
StringBuffer sb = new StringBuffer(strOrig);
String toReplace = "";

if (strReplace == null) toReplace = "";
else toReplace = strReplace;

int pos = strOrig.length();
while (pos > -1) {
pos = strOrig.lastIndexOf(strFind, pos);
if (pos > -1) sb.replace(pos, pos+strFind.length(), toReplace);
pos = pos - strFind.length();
}
return sb.toString();
}

The method replaceString returns a String and takes in 3 parameters. First would be the original string where you want your search and replace to take place into. Second is be the string to search for and third is the replace string.

Javascript has a different approach. Its replace() function looks a bit like regex code but it does not work by accepting regex combinations. To use the replace() function in Javascript, enclose your string to search with backslashes like this.


var str = 'this is a string';
str.replace(/is/ig, 'replaced');

The replace function contains two options. ‘i’ means the search would be case insensitive and ‘g’ means global, meaning it will search all instances of the search string within the str variable. If you do not include ‘g’ as its option, then only the first instance of the search string will be replaced.

Java and Javascript should have provided simple replace methods that greatly help newbies to these languages. The replaceString() method in Java is a good workaround for easy to use search and replace purposes in java.

No TweetBacks yet. (Be the first to Tweet this post)

Like what you see? Buy me a cup of coffee. Or subscribe to my feeds.


(No Ratings Yet)
 Loading ...

2 Responses to “Replacing strings with Java and Javascript”

  1. 1
    Christian Ullenboom Says:

    Isn’t String#replace(CharSequence, CharSeqence) the method you are looking for? It’s a method new with Java 5. Under the hood regex are replacing so the performance can be degraded. Christian

  2. 2
    chitgoks Says:

    ahh.. ive never checked the 1.5 API for that since ive been with 1.4 since. Though im using 1.6 now i’ve never bothered looking if there are any new methods in 1.6 since I’ve been using that workaround method I have. quite useful for me? will check that CharSequence thing. Might be more performance-better than my method.

Leave a Reply