This String replace example in Java will show you how to replace String in Javaboth at character level and by using regular expression. Since String is final in Java every time you replace String you will get a new String object only if your actually replace anything on original String otherwise replace methods of String return same String object. String Class in Javaprovides 4 methods to replace String in Java. Those methods allow you to replace character from String,replace CharacterSequence from String, replace all occurrence of pattern in String or just firstoccurrence of any pattern in Java. We will also see important points on String replace method andhow to make best use of regular expression while replace string in Java.
This String replace method replaces one character sequence with other. This method has added fromJDK 1.5 onwards. Here is aString replace example of replacing character sequence form String
String replaceSample = "String replace Example of replacing Character Sequence";
Output: String REplace Example of REplacing Character Sequence
In this String replace Example, we have replaced "re" with "RE".
1) This String replace method will throw NullPointerException if either oldCharSequence or newCharSequenceis null.
2) Replacement of String starts from beginning and proceed towards end. So in a String "ccc" replacing"cc" with "d" will result in "dc" rather than "cd".
3. Replace method to replace all matched pattern in String
replaceAll(String regex, String replacement)
Good thing about replace method of String Class in Javais that it supports regular expression. With regex capability you can perform sophisticated Search on String and than replace characters. This String replace methodreplaces each matched substring with the replacement String provided. Let's see String replace examplewith regular expression:
String replaceSample = "String replace Example with regular expression";
Output: Rtring replace Example with regular expression
This String replace replaces any "S" wiht "R" if it comes at beginning of line "^" denotes beginning of line you can also apply pattern matching wildcards and charset while replacing String in Java.
1. This String replace method will throw PatternSyntaxException in case regular expression's syntax is not valid.
4. Replace method to replace first matched pattern in String
replaceFirst(String regex, String replacement)
This String replace method is similar to above method but it only replaces first occurrence of matchingpattern instead of all occurrence. Very handy some time. Here is an example of String replace withreplaceFirst() method.
String replaceSample = "String replace Example with replaceFirst";
You can see only first occurrence of "re" is replaced with "RE" while second occurrence remains same
That’s all on how to replace String in Java, search and replace is one of the most needed thing in String and having an idea of right way of doing it is good. These String replace examples are rather simple but you can make more sophisticated by using regular expression.
No comments:
Post a Comment