Java String Replace Example Tutorial
来源:互联网 发布:淘宝客活动报名技巧 编辑:程序博客网 时间:2024/06/03 02:26
This String replace example in Java will show you how to replace String in Java both at character level and by using regular expression.SinceString 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 replacemethods of Stringreturn same String object.String Class in Javaprovides 4 methods to replace String in Java. Thosemethods allow you to replace character from String,replace CharacterSequence fromString, replace all occurrence ofpattern 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 ofregular expression while replace string in Java.
This article is on series of my other String article like2 ways to Split String in Java andhow to convert String to Date in Java. String is one of the most important classes in Java and having a good knowledge of String class is mandatory for any Java developer.
Java String Replace Example and Tutorial in Java
String Replace Examples in Java
As I said earlier Java provides at-least four methods toreplace String in Java according to JDK 1.6documentation.
1. Replace method to replacesingle character in String
replace(char oldChar, char newChar)
This replace method in String takes one character and replaces all of its occurrence in provided
String with new character provided to it. Here is anString replace Example of changing character
String replaceSample = "This String replace Example shows how to replace one char from String";
String newString = replaceSample.replace('r', 't');
Output: This Stting teplace Example shows how to teplace one chat ftom Stting
You can see all occurrence of "r" has been replaced by "t".
Important points:
1. This replace method of String will return a new String object if there is a replace.
2. It will return same String objects if there is no matching char and there is no replace.
3. Replacement of String is Case Sensitive, so replacing "c" will only replace small case not CapitalCase.
2. Replace method to replace character sequence in String
replace(CharSequence target, CharSequence replacement)
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";
String newString = replaceSample.replace("re", "RE");
Output: String REplace Example of REplacing Character Sequence
In this String replace Example, we have replaced "re" with "RE".
Important points:
1) This String replace method willthrowNullPointerException if either oldCharSequence or newCharSequenceis null.
2) Replacement of Stringstarts from beginning and proceed towards end. So ina 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 StringClass in Javais that it supports regular expression.With regex capability you can perform sophisticatedSearch 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";
String newString = replaceSample.replaceAll("^S","R");
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 alsoapply pattern matching wildcards and charset while replacing String in Java.
Important points:
1. This String replace method will throwPatternSyntaxException in caseregular 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";
String newString = replaceSample.replaceFirst("re","RE");
Output: 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.
Other Java Tutorials
How to Convert Date to String in Java
How to Convert Sting to double in Java
How to override CompareTo in Java
How to override Equals in java
How to implement Thread in Java
Spring Interview questions in java
JSP Interview questions in java
Read more: http://javarevisited.blogspot.com/2011/12/java-string-replace-example-tutorial.html#ixzz2ki3ClW3k
- Java String Replace Example Tutorial
- Java String.replace()方法
- Java String.replace()方法
- Java String Split Example
- Java Design Patterns – Example Tutorial
- Java String.replace()的方法
- Java String.replace() VS replaceAll()
- java string 字符串替换:replace
- Java ExecutorService Example – Tutorial(java线程池)
- 10 Example of Hashtable in Java – Java Hashtable Tutorial
- Java Servlet Filter tutorial example using Eclipse & Tomcat
- How to use ConcurrentHashMap in Java - Example Tutorial and Working
- How to override compareTo method in Java - Example Tutorial
- The Java EE 6 Tutorial The mood Example Application
- Tutorial: DTrace by Example
- CGICC:A Tutorial Example
- ANDROID STAGGEREDGRIDLAYOUTMANAGER EXAMPLE TUTORIAL
- 火星又归来: String.replace("SUNW", "JAVA")
- How to create Immutable Class and Object in Java - Tutorial Example
- tomcat无法启动之tj
- build webrtc for android
- 技术人员谈管理之采购管理案例论文
- 非阻塞算法在并发容器中的实现
- Java String Replace Example Tutorial
- Difference between Stack and Heap memory in Java
- How to use ConcurrentHashMap in Java - Example Tutorial and Working
- 易混淆的字母
- nyoj-79-拦截导弹
- 关于日期2013-05-25T19:09:18.769+0800格式化
- 【黑马程序员】 java笔记——抽象类
- RGB图像灰度化
- 【ASP.Net MVC3 】使用Moq让单元测试变得更简单