Java FAQ Java String substitution method uses the

Source: Internet
Author: User


The following code:

public class Example020 {public static void main (string[] args) {String separator = File.separator; String clazzname = Example020.class.getName (); String rs1 = Clazzname.replace (".", separator); Method 1String rs2 = Clazzname.replaceall ("\ \", "\\\\");//method 2String Rs3 = Clazzname.replaceall (Pattern.quote ("."), Matcher.quotereplacement (separator));//Method 3system.out.println ("Class name =" + Clazzname); System.out.println ("rs1=" + rs1); System.out.println ("rs2=" + rs2); System.out.println ("rs3=" + Rs3);}}

Analysis Description:

The above code is three ways to replace Java strings. Java provides two methods for string substitution since version 1.5: Replace and ReplaceAll. Both methods have the same functionality and are able to replace all the characters in the string that need to be replaced (although the naming is confusing). The difference is that theparameters passed in by the Replace method are the characters to be replaced and the substituted character, and the ReplaceAll pass in a regular representation of the character to be replaced and the replacement.

Using ReplaceAll, the second parameter passed in the "\ \" unfriendly error hint exception in thread "main" java.lang.StringIndexOutOfBoundsException:String index Out of Range:1at java.lang.String.charAt (string.java:658) at Java.util.regex.Matcher.appendReplacement (Matcher.java : 762) at Java.util.regex.Matcher.replaceAll (matcher.java:906) at Java.lang.String.replaceAll (string.java:2162) at Cn.itape.java.disabuse.Example020.main (example020.java:13)
    • In Method 3, from 1.5, Java provides two methods, the string directly into the form of regular expressions, directly use, to avoid the need to join in Method 2 to escape, reduce the likelihood of errors.


(note: This "Java doubts" series, are bloggers read the original book "Java Doubts", the original book on the explanation and examples of the adaptation, and then written blog post. All examples are tested in person and shared on GitHub. Use these examples to motivate yourself and benefit others. At the same time, all posts in this series will be posted in the blogger's personal public number (search for "Love Ape" or "ape_it") for easy reading. If there is any content that infringes the rights of the original author, please inform the blogger in time, in order to delete it in time, if the reader has objection or question to the content of the text, welcome via blog post or public message, etc. co-exploration. )

Source code Address: Https://github.com/rocwinger/java-disabuse


This article from "Winger" blog, declined reprint!

Java FAQ Java String substitution method uses the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.