String Replace and String Split in Java with Examples

 

We will discuss String Replace and String Split in Java with some examples.

 

String Replace

String replace is the process by which we replace parts of a string with another string. Some Java methods directly accept character sequences, while some others accept a regular expression (regex). We will discuss some of these approaces using examples.

 

Case 1 - Replacing a metacharacter (.) with a normal string

 

 We want to replace all dots (.) in a string that represents a package (e.g com.javajee.abc) with forward slash (/) to display the corresponding folder structure (e.g. com/javajee/abc) of a package declaration. 

 

 

Solution 1 - Using replaceAll() method of String 

What will be the output of the below code?

String testPackage = "com.test.pack";

System.out.println( testPackage.replaceAll(".", "/") );

Output 

/////////////.class.

This is because String.replaceAll takes a regular expression as its first parameter, not a sequence of characters. The signature of replaceAll() is replaceAll(String regex, String replacement). In regular expression the metacharacter dot (.) matches any single character, and so every character of com.test.pack is replaced by a slash, producing the above output.

To match for a metacharacter, we need to first treat it as a regular character. To do this we need to escape it with a backslash (\) as "\." in the regex. However, in Java also, a backslash (\) begins an escape sequence and mainly used to escape characters like double quote in a string literal. So if you try to use "\.", java will compare it with the escapes available in java and complain that there is no such escape sequence in java (\.). Therefore the backslash itself must be escaped with a second backslash to tell java to escape the \ itself and make it available to the regex as "\.". 

Final Solution using replaceAll() is:

System.out.println(testPackage.replaceAll("\\.", "/"));

Will print com/test/pack

To see all escape sequences in java, please refer to escape-sequences-in-java-with-examples.

However, at least for some, the above rules might be confusing. So java has come up with an easier solution as we will see next.

 

Alternate Solution (Better Approach) - Using Pattern.quote(".")

Java 5.0 provides the new static method java.util.regex.Pattern.quote that takes a string as a parameter and adds any necessary escapes by its own, returning a regular expression string that matches the input string exactly. 

System.out.println(testPackage.replaceAll(Pattern.quote("."), "/"));

This will print the correct output and you don't have to worry about escaping. 

 

Case 2 - The replacement string also needs escaping

In the previous example we used / as the replacement string, which is an unix file separator. We will use File.separator to print platform specifc file seperator.  

package com.test;

public class Test {

   public static void main(String[] args) {

      System.out.println( Test.class.getName().replaceAll("\\.", File.separator) + ".class");

   }

}

As we have seen, If the file separator is a slash, as it is on UNIX, the program prints com/test/Test.class. However, if the file separator is a backslash, as it is on Windows, the program throws an exception:

StringIndexOutOfBoundsException: String index out of range: 1

 at java.lang.String.charAt

 at java.util.regex.Matcher.appendReplacement

 at java.util.regex.Matcher.replaceAll

 at java.lang.String.replaceAll

The second parameter of String.replaceAll is a replacement string and a backslash appearing in a replacement string escapes the following character, which is the ending double quotes. Therefore a lone backslash character as a replacement string is invalid.

 

Solution 1 - Using String.replace()

The String.replace(CharSequence, CharSequence), does the same thing as String.replaceAll, but treats both the pattern and the replacement as character sequences. 

replace(".", File.separator) + ".class");

String.replace replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

 

Solution 2 - Using Matcher.quoteReplacement()

Similar to Pattern.quote method, use java.util.regex.Matcher.quoteReplacement, which translates a string into the corresponding replacement string.

replaceAll(Pattern.quote("."),  Matcher.quoteReplacement(File.separator))+ ".class");

 

String Split

String split is the process by which a string is split around a delimiter. 

 

Example 1

String test = "ABC.   DEF. XYZ.";

To get the output as "ABC", "DEF", and "XYZ", we can split the string with a delimeter as a 'dot(.) followed by zero or more whitespace characters'.  As we have seen dot(.) needs to be represented as \\.. Similarly, we can represent zero or more whitespace characters with proper escaping as \\s*, where \s denotes whtespace character and \s* denote zero or more whitespace characters in regex.

Therefore the final solution is:

String[] result = test.split("\\.\\s*");

Please check out http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html for more details on regex in java

 

Example 2

If \d denotes a digit in regex, what will be the output of below program:

String test = "a1b2c3";

String[] tokens = test.split("\\d");

for(String s: tokens) System.out.print(s + " ");

Output will be:

a b c

 

NOTE: Be careful when using unfamiliar library methods. When in doubt, consult the Javadoc. Also, regular expressions are tricky: Problems tend to show up at run time rather than compile time.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (8) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (12) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)