Ví dụ này sẽ mô tả các phương pháp thay thế(replace) của lớp(Class) String để thay đế các ký tự hay chuỗi bằng các ký tự hoặc chuỗi mới trong java.
StringReplace.java
/**
* @(#)StringReplace.java
*
* StringReplace application
*
* @author BUI NGOC SON
* @version 1.00 2013/11/14
*/
public class StringReplace {
public static void main(String[] args) {
String str="Hello World";
System.out.println( str.replace( 'H','W' ) );
System.out.println( str.replaceFirst("He", "Wa") );
System.out.println( str.replaceAll("He", "Ha") );
}
}
* @(#)StringReplace.java
*
* StringReplace application
*
* @author BUI NGOC SON
* @version 1.00 2013/11/14
*/
public class StringReplace {
public static void main(String[] args) {
String str="Hello World";
System.out.println( str.replace( 'H','W' ) );
System.out.println( str.replaceFirst("He", "Wa") );
System.out.println( str.replaceAll("He", "Ha") );
}
}
Sau khi chạy chương trình bạn sẽ được kết quả như hình bên dưới.
No comments:
Post a Comment