Java differences in the replacement string specific character, replaceAll, replace, replaceFirst of

 

Use ";" replaced over the string ","

public class Test01 {
public static void main(String[] args) {
String number = "123,456,5234,52345,63456,7456,7";
String newNumber = number.replace(",", ";");
System.out.println(newNumber);
}

}

result:

123;456;5234;52345;63456;7456;7

 

Difference replaceAll, replace, replaceFirst of

String strTmp = new String("BBBBBBBYYYYYYY");


replaceAll supports regular expressions and alternative character
strTmp strTmp.replaceAll = ( "\\ D", "the Y");
System.out.println (strTmp);
strTmp = strTmp.replaceAll ( "the Y", "N");
the System .out.println (strTmp);


Alternatively support character string and replace
strTmp strTmp.replace = ( "N", "C");
System.out.println (strTmp);


Alternatively only the first character
strTmp strTmp.replaceFirst = ( "\\ D", "Q");
System.out.println (strTmp);

public class Main {

    public static void main(String[] args) {
	    String strTmp = new String("BBBBYYYYYY");

	    strTmp = strTmp.replaceAll("\\D","Y");
	    System.out.println(strTmp);
        strTmp = strTmp.replaceAll("Y","N");
        System.out.println(strTmp);

        strTmp = strTmp.replace("N","C");
        System.out.println(strTmp);

        strTmp = strTmp.replaceFirst("\\D","q");
        System.out.println(strTmp);
    }
}

 result:

YYYYYYYYYY
NNNNNNNNNN
CCCCCCCCCC
qCCCCCCCCC

 

 

 

 

Guess you like

Origin www.cnblogs.com/csushl/p/11946455.html
Recommended