|
package ss1;
/** *String類的判斷功能: *public boolean equals(Object anObject) 將此字符串與指定的對象比較,內(nèi)容是否相同,區(qū)分大小寫 *public boolean equalsIgnoreCase(String anotherString) 將此字符串與指定的對象比較,內(nèi)容是否相同,不區(qū)分大小寫 *public boolean contains(CharSequence s) 判斷大字符串中是否包含小字符串 *public boolean startsWith(String prefix) 測試此字符串是否以指定的前綴開始。 *public boolean endsWith(String suffix) 測試此字符串是否以指定的后綴結(jié)束。 *public boolean isEmpty() 判斷字符串是否為空,當(dāng)且僅當(dāng) length() 為 0 時返回 true。 *注意: * 字符串內(nèi)容為空和字符串對象為空. * String s =""; * String s1 = null; */ public class Menu { public static void main(String[] args) { String s1 = "helloworld"; String s2 = "helloworld" ; String s3 = "Helloworld"; System.out.println(s1.equals(s2)); //true System.out.println(s1.equals(s3)); //false System.out.println(s1.equalsIgnoreCase(s3)); //true System.out.println(s1.contains("hell")); //true System.out.println(s1.contains("hw")); //false System.out.println(s1.startsWith("hello")); //true System.out.println(s1.startsWith("h")); //true System.out.println(s1.endsWith("ld")); //true String s =""; String ss = null; System.out.println(s1.isEmpty()); //false System.out.println(s.isEmpty()); //true System.out.println(ss.isEmpty()); //NullPointerException 原因是ss未創(chuàng)建對象,無法調(diào)方法。 } } 結(jié)果: true false true true false true true true false true Exception in thread "main" java.lang.NullPointerException at ss1.Menu.main(Menu.java:35) |
|
|