Bài viết này sẽ hướng dẫn các phương pháp dùng để so sánh hai chuỗi(String) trong ngôn ngữ lập trình java.
Cách 1: Sử dụng phép toán so sánh bằng.
String str_01 = "VNLIVES.NET";
String str_04 = "VNLIVES.NET";
if (str_01 == str_04) {
System.out.println("[==] str_01 and str_04 are same value!");
} else {
System.out.println("[!=] str_01 and str_04 are different!");
}
String str_04 = "VNLIVES.NET";
if (str_01 == str_04) {
System.out.println("[==] str_01 and str_04 are same value!");
} else {
System.out.println("[!=] str_01 and str_04 are different!");
}
Cách 2: Sử dụng phép toán so sánh không bằng.
String str_01 = "VNLIVES.NET";
String str_02 = "VNLIVES";
if (str_01 != str_02) {
System.out.println("[!=] str_01 and str_02 are different!");
} else {
System.out.println("[==] str_01 and str_02 are same value!");
}
String str_02 = "VNLIVES";
if (str_01 != str_02) {
System.out.println("[!=] str_01 and str_02 are different!");
} else {
System.out.println("[==] str_01 and str_02 are same value!");
}
Cách 3: Sử dụng phương thức so sánh "equals()", lưu ý đối với phương pháp so sánh này "equals" sẽ khôn phân biệt hoa thường.
String str_02 = "VNLIVES";
String str_03 = "vnlives";
if (str_02.equals(str_03)) {
System.out.println("[equals] str_02 and str_03 are different!");
} else {
System.out.println("[equals] str_02 and str_03 are same value!");
}
String str_03 = "vnlives";
if (str_02.equals(str_03)) {
System.out.println("[equals] str_02 and str_03 are different!");
} else {
System.out.println("[equals] str_02 and str_03 are same value!");
}
Cách 4: Sử dụng phương thức so sánh "equalsIgnoreCase()"
if (str_02.equalsIgnoreCase(str_03)) {
System.out.println("[equalsIgnoreCase] str_02 and str_03 are different!");
} else {
System.out.println("[equalsIgnoreCase] str_02 and str_03 are same value!");
}
System.out.println("[equalsIgnoreCase] str_02 and str_03 are different!");
} else {
System.out.println("[equalsIgnoreCase] str_02 and str_03 are same value!");
}
Dưới đây là mã ví dụ toàn bộ phương thức so sanh trên.
/**
* @(#)JavaCompareTwoString.java
*
* JavaCompareTwoString application
*
* @author BUI NGOC SON
* @version 1.00 2014/2/24
*/
public class JavaCompareTwoString {
public static void main(String[] args) {
String str_01 = "VNLIVES.NET";
String str_02 = "VNLIVES";
String str_03 = "vnlives";
String str_04 = "VNLIVES.NET";
if (str_01 == str_04) {
System.out.println("[==] str_01 and str_04 are same value!");
} else {
System.out.println("[!=] str_01 and str_04 are different!");
}
if (str_01 != str_02) {
System.out.println("[!=] str_01 and str_02 are different!");
} else {
System.out.println("[==] str_01 and str_02 are same value!");
}
if (str_02.equals(str_03)) {
System.out.println("[equals] str_02 and str_03 are different!");
} else {
System.out.println("[equals] str_02 and str_03 are same value!");
}
if (str_02.equalsIgnoreCase(str_03)) {
System.out.println("[equalsIgnoreCase] str_02 and str_03 are different!");
} else {
System.out.println("[equalsIgnoreCase] str_02 and str_03 are same value!");
}
}
}
* @(#)JavaCompareTwoString.java
*
* JavaCompareTwoString application
*
* @author BUI NGOC SON
* @version 1.00 2014/2/24
*/
public class JavaCompareTwoString {
public static void main(String[] args) {
String str_01 = "VNLIVES.NET";
String str_02 = "VNLIVES";
String str_03 = "vnlives";
String str_04 = "VNLIVES.NET";
if (str_01 == str_04) {
System.out.println("[==] str_01 and str_04 are same value!");
} else {
System.out.println("[!=] str_01 and str_04 are different!");
}
if (str_01 != str_02) {
System.out.println("[!=] str_01 and str_02 are different!");
} else {
System.out.println("[==] str_01 and str_02 are same value!");
}
if (str_02.equals(str_03)) {
System.out.println("[equals] str_02 and str_03 are different!");
} else {
System.out.println("[equals] str_02 and str_03 are same value!");
}
if (str_02.equalsIgnoreCase(str_03)) {
System.out.println("[equalsIgnoreCase] str_02 and str_03 are different!");
} else {
System.out.println("[equalsIgnoreCase] str_02 and str_03 are same value!");
}
}
}
Kết quả:
[==] str_01 and str_04 are same value!
[!=] str_01 and str_02 are different!
[equals] str_02 and str_03 are same value!
[equalsIgnoreCase] str_02 and str_03 are different!
[!=] str_01 and str_02 are different!
[equals] str_02 and str_03 are same value!
[equalsIgnoreCase] str_02 and str_03 are different!
No comments:
Post a Comment