好久沒寫博客了,一直在糾結后面的路怎么發展?好了不說廢話了!!正題開始!!
String與StringBuffer類是我們在開發中最常用的,我們現在一起來分析一下這兩個類,首先我們先來談談String的方法:
String s = “123456”;
1、length()方法:
這個方法是獲取字符串長度的方法,常常用于登陸注冊頁中判斷用戶輸入的字符長度是否合法;
例:
String pswd = "abc1234567";
if(pawd.length()>6){
System.out. } 2、equals()方法 java中附帶了兩種字符串比較方法,第一種是equals,這個是嚴格型的比較,他區分大小;第二種是equalsIsnoreCase(),它是用來判斷字符串不區分大小寫的比較 他們都是比較的字符串的值,而"=="是判斷字符串在內存中的地址,這兩者之間是有區別的; 例: equals方法例子: String b = "abc123"; if (b.equals(c)) { equalsIsnoreCase方法例子: String b = "abc123"; "=="使用例子: String b = "abc123"; 3、toLowerCase()和toUpperCase()的方法使用 toLowerCase()是把字符串轉化為小寫形式 toUpperCase()是把字符串轉化為大寫形式 例子: String b = "abc123"; 4、split()方法 作用是把一個字符串拆分為多個字符串 例: String c = "abc-123"; 5、indexOf()方法 它的作用是尋找你所查找的字符是在字符串中的那個位置,從零個下標開始 String b = "abc123"; 今天就寫在這里咯,下面提幾個有意思的問題,大家可以再評論中回答: 1、equals和"=="有什么區別? 2、有一個座機號碼:0731-76333665,我需要知道它的區號和座機號(區號四位,座機號7位)是否符合規范該如何操作? 下面推薦一個字符串的博客:http://m.survivalescaperooms.com/YSO1983/archive/2009/12/07/1618564.html;
String c = "ABC123";
System.out.println("相等");
} else {
System.out.println("不相等");
}
String c = "ABC123";
if (b.equalsIgnoreCase(c)) {
System.out.println("相等");
} else {
System.out.println("不相等");
}
String c = "abc123";
if (b==c) {
System.out.println("相等");
} else {
System.out.println("不相等");
}
String c = "abc123";
b.toLowerCase();
c.toUpperCase();
System.out.println(b);
System.out.println(c);
String [] d = c.split("-");
System.out.println(d[0]);
System.out.println(d[1]);
String c = "abc-123";
int d = c.indexOf("2");
System.out.println(d);
新聞熱點
疑難解答