學(xué)習(xí)內(nèi)容
1不可變字符串
String
- 1.不可變的字符串 一旦創(chuàng)建 內(nèi)容不能改變
- 用“==”比較兩個(gè)對(duì)象是否相同(地址)
“equals”比較內(nèi)容是否相同
- 用“==”比較兩個(gè)對(duì)象是否相同(地址)
- 3.字符串的創(chuàng)建
a.String str="";
b.String str =new String();
構(gòu)造方法 沒意義
c.使用字節(jié)數(shù)組 創(chuàng)建一個(gè)字符串/一串?dāng)?shù)字
byte[] name={'a','b','c'};
String str=new String(name);
System.out.println(str);
byte[] name2 = {97,98,99};
String str = new String(name2);
System.out.println(str);
e.使用字節(jié)數(shù)組的一部分 創(chuàng)建一個(gè)字符串
String str=new String(name,0,2);
System.out.println(str);
- 字符串有哪些方法
1.獲取字符串中的一個(gè)字符 chatAt
char c=h.charAt();
System.out.println(c);
2.比較兩個(gè)字符串的大小 compareTo (0:相同钱床, >0:大于昔汉,<0:小于)
int result=str1.compareTo(str2);
System.out.println(result);
3.兩個(gè)字符串的連接 concat
String nStr=str.concat(str2);
System.out.println(nStr);
4.判斷一個(gè)字符串是否包含另一個(gè)字符串 contains
boolean r="hello".contains("lle");
System.out.println(r);
5.判斷是否以某個(gè)字符串結(jié)尾/開 endsWith/startsWith
String url="http://www.baidu.com";
if(url.endsWith(".com")){
System.out.println("網(wǎng)址");
}
if(url.startsWith("http")){
System.out.println("http協(xié)議");
}
if(url.startsWith("www",7)){
System.out.println("萬維網(wǎng)");
}
6.兩個(gè)字符串進(jìn)行比較 equals
if ("abc".equals("ABC")){
System.out.println("相同");
}
else{
System.out.println("不相同");
}
7.判斷一個(gè)字符串在另一個(gè)字符串里面的位置(不存在返回值為-1) indexOf
String i1="hello Java";
int index=i1.indexOf("Javee");
System.out.println(index);
8.獲取子字符串 substring
9.將字符串轉(zhuǎn)化為字符數(shù)組 toCharArray
10.將所有字符轉(zhuǎn)化為小寫/大寫toLowerCase/toUpperCase
11.將字符串其那面和后面的空格字符刪除 trim()
2可變字符串
StringBuffer 線程安全 效率不高
StringBuilder 線程不安全的 效率更高
需要了解的方法:insert append delete replace
// 創(chuàng)建的同時(shí)先準(zhǔn)備好6個(gè)字符的空間
StringBuilder sb = new StringBuilder(6);
// append 在末尾追加
sb.append("I");
sb.append(" Love");
sb.append(" Android");
System.out.println(sb);
// insert 插入數(shù)據(jù)
sb.insert(2, "also ");
System.out.println(sb);
// replace 替換
// start end string
int start = sb.indexOf("Android");
int end = start + "Android".length();
sb.replace(start,end,"you!!!");
System.out.println(sb);
// reverse 反轉(zhuǎn)
sb.reverse();
System.out.println(sb)
如何判斷是設(shè)計(jì)普通類蛤签、抽象類還是接口呢?
抽象類 | 普通類 | 接口 | ||
---|---|---|---|---|
是否需要添加成員變量型将? | 需要 | ?? | ?? | ? |
不需要 | ? | ? | ?? | |
添加的方法是不是必須要實(shí)現(xiàn)寂祥? | 必須 | ?? | ? | ?? |
不需要 | ?? | ?? | ? | |
需要提供模板還是通信的工具? | 模板 | ?? | ? | ? |
通信(數(shù)據(jù)傳遞) | ? | ? | ?? |
心得體會(huì)
暫時(shí)沒有