![]() Solution 2 - Using Matcher.quoteReplacement() 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". ![]() String.replace replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The String.replace(CharSequence, CharSequence), does the same thing as String.replaceAll, but treats both the pattern and the replacement as character sequences. Therefore a lone backslash character as a replacement string is invalid. ![]() 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. StringIndexOutOfBoundsException: String index out of range: 1Īt .appendReplacement However, if the file separator is a backslash, as it is on Windows, the program throws an exception: ( ().replaceAll("\\.", parator) ".class") Īs we have seen, If the file separator is a slash, as it is on UNIX, the program prints com/test/Test.class. We will use parator to print platform specifc file seperator. In the previous example we used / as the replacement string, which is an unix file separator. This will print the correct output and you don't have to worry about escaping.Ĭase 2 - The replacement string also needs escaping (testPackage.replaceAll( Pattern.quote("."), "/")) Java 5.0 provides the new static method .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. ![]() So java has come up with an easier solution as we will see next.Īlternate Solution (Better Approach) - Using Pattern.quote(".") However, at least for some, the above rules might be confusing. To see all escape sequences in java, please refer to escape-sequences-in-java-with-examples. 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 "\.". However, in Java also, a backslash (\) begins an escape sequence and mainly used to escape characters like double quote in a string literal. To do this we need to escape it with a backslash (\) as "\." in the regex. To match for a metacharacter, we need to first treat it as a regular character. In regular expression the metacharacter dot (.) matches any single character, and so every character of is replaced by a slash, producing the above output. The signature of replaceAll() is replaceAll(String regex, String replacement). This is because String.replaceAll takes a regular expression as its first parameter, not a sequence of characters. What will be the output of the below code? Solution 1 - Using replaceAll() method of String We want to replace all dots (.) in a string that represents a package (e.g ) with forward slash (/) to display the corresponding folder structure (e.g. com/javajee/abc) of a package declaration. ![]() We will discuss some of these approaces using examples.Ĭase 1 - Replacing a metacharacter (.) with a normal string Some Java methods directly accept character sequences, while some others accept a regular expression (regex). String replace is the process by which we replace parts of a string with another string. We will discuss String Replace and String Split in Java with some examples. ![]()
0 Comments
Leave a Reply. |