8.13 集訓第十三天 【】

學習內(nèi)容

1不可變字符串
String

  • 1.不可變的字符串 一旦創(chuàng)建 內(nèi)容不能改變
    1. 用“==”比較兩個對象是否相同(地址)
      “equals”比較內(nèi)容是否相同
  • 3.字符串的創(chuàng)建
    a.String str="";
    b.String str =new String();構(gòu)造方法 沒意義
    c.使用字節(jié)數(shù)組 創(chuà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)建一個字符串

String str=new String(name,0,2);
System.out.println(str);
  • 字符串有哪些方法
    1.獲取字符串中的一個字符 chatAt
char c=h.charAt();
System.out.println(c);

2.比較兩個字符串的大小 compareTo (0:相同, >0:大于,<0:小于)

int result=str1.compareTo(str2);
System.out.println(result);

3.兩個字符串的連接 concat

String nStr=str.concat(str2);
System.out.println(nStr);

4.判斷一個字符串是否包含另一個字符串 contains

boolean r="hello".contains("lle");
System.out.println(r);

5.判斷是否以某個字符串結(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.兩個字符串進行比較 equals

if ("abc".equals("ABC")){
System.out.println("相同");
    }
else{
System.out.println("不相同");
    }

7.判斷一個字符串在另一個字符串里面的位置(不存在返回值為-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)建的同時先準備好6個字符的空間
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)

\color{red}{*}如何判斷是設計普通類、抽象類還是接口呢?

抽象類 普通類 接口
是否需要添加成員變量? 需要 ?? ?? ?
不需要 ? ? ??
添加的方法是不是必須要實現(xiàn)? 必須 ?? ? ??
不需要 ?? ?? ?
需要提供模板還是通信的工具? 模板 ?? ? ?
通信(數(shù)據(jù)傳遞) ? ? ??

心得體會

暫時沒有

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容