String類的常見操作,String類常見操作

來源:互聯網
上載者:User

String類的常見操作,String類常見操作

一.String類概述

1.String的值是一個對象,也是一個常量不能被改變

2.String的equals方法是比較兩個字串的內容

3.String s1=”abc”  String s2=new String(“abc”) s1有一個對象,s2有兩個對象

二.String常見功能---擷取和判斷

1.擷取

1.1 字串中包含的字元數,也就是字串的長度。

int length():擷取長度。

1.2 根據指定位置擷取位置上的某個字元

char charAt(int index)

1.3 根據字元擷取該字元在字串中的位置

Int indexOf(int ch):返回的是ch在字串中第一次出現的位置

int indexOf(int ch,int fromIndex):從fromIndex指定位置開始,擷取ch在字串中出現的位置

int indexOf(String str,int fromIndex): 從fromIndex指定位置開始,擷取ch在字串中出現的位置,如果沒有找到返回-1

int lastIndexOf(String str):反向索引一個字元出現的位置

1 public static void method_get(){ 2 String str="abcdeakpf"; 3 //列印字串的長度 4 sop(str.length()); //結果為5 5 //根據索引擷取字元 6 sop(str.charAt(4)); //結果為a 7 //根據字元擷取索引 8 sop(str.indexOf('a', 3)); //結果為5 9 //反向索引一個字元出現的位置10 sop(str.lastIndexOf("a")); //結果為511 }View Code

2.判斷

2.1 字串中是否包含某一個子串

boolean contains(str)

特殊之處:indexOf(str):可以索引str第一次出現的位置,如果返回-1,表示該str不在字串中存在,所以也可以用於判斷是否包含指定字元子串,而且可以擷取該子串出現的位置;

2.2 字元中是否有內容

boolean isEmpty():原理就是判斷length是否為0

2.3 字串中是否以指定內容開頭

boolean startsWith(str);

2.4 字串是以指定內容結尾

boolean endsWith(str);

2.5 判斷字串內容是否相同,複寫了Object類中的equals方法

boolean equals(str);

2.6 判斷內容是否相同,忽略大小寫

boolean equalsIgnoreCase();

1 public static void method_is(){2 String str="ArrayDemo.java";3 //判斷檔案名稱是否以Array單詞開頭4 sop(str.startsWith("Array")); //結果為true5 //判斷檔案名稱是否是.java檔案6 sop(str.endsWith(".java")); //結果為true7 //判斷檔案名稱中是否包含Demo單詞8 sop(str.contains("Demo")); //結果為true9 }View Code

3.字串常見功能---轉換

3.1 將字元數組轉成字串。

建構函式:String(char[])

          String(char[],offset,count):將字元數組中的一部分轉成字串

靜態方法:

          static String copyValueOf(char[])

          static String copyValueOf(char[],offset,count)

          static String valueOf(cahr[])

3.2 將字串轉成字元數組

char[] toCharArray();

3.3 將位元組數群組轉換成字串

          String(byte[])

          String(byte[],offset,count):將位元組數組中的一部分轉成字串

3.4 將字串轉換成位元組數組

byte[] getBytes();

3.5 將基礎資料型別 (Elementary Data Type)轉換成字串

static String valueOf(int)

static String valueOf(double)

3.6 特殊:字串和位元組數組在轉換過程中是可以指定編碼錶的。

1 public static void method_trans(){ 2 char[] arr={'a','b','c','d','e','f'}; 3 String s=new String(arr,1,3); 4 sop("s="+s); //結果為bcd 5 6 String s1="zxcvbnm"; 7 char[] chs=s1.toCharArray(); 8 for (int i = 0; i < chs.length; i++) { 9 sop("ch="+chs[i]);10 }11 }View Code

4.替換

String replace(oldchar,   newchar)

如果要替換的字元不存在,返回的還是原串

1 public static void method_replace(){2 String s="Hello java";3 String s1=s.replace('a', 'n');4 String s2=s.replace("java", "world");5 sop("s="+s); //結果為Hello java6 sop("s1="+s1); //結果為Hello jnvn7 sop("s2="+s2); //結果為Hello world8 }View Code

5.切割

String[] split(regex);

1 public static void method_split(){2 String s="zhangsan,lisi,wangwu";3 String[] attr=s.split(",");4 for (int i = 0; i < attr.length; i++) {5 sop(attr[i]); //結果為zhangsan lisi wangwu6 }7 }View Code

6.子串,擷取字串中的一部分

String substring(begin):從指定位置開始到結尾,如果角標不存在,會出現角標越界異常

String substring(begin,end):從起始位置開始到結束位置為止,保護起始位置不包含結束位置

1 public static void method_sub(){2 String s="abcdef";3 sop(s.substring(2)); //結果為cdef4 sop(s.substring(2, 4)); //結果為cd5 }View Code

7.轉換,去除空格,比較

7.1 將字串轉換成大寫或者小寫

String toUpperCase();

String toLowerCase();

7.2 將字串兩端的多個空格去除

String trim();

7.3 對兩個字串進行自然順序的比較

int compareTo(String)

1 public static void method_7(){2 String s=" Hello Java ";3 sop(s.toUpperCase());4 sop(s.toLowerCase());5 sop(s.trim());6 String s1="acc";7 String s2="aaa";8 sop(s1.compareTo(s2)); //結果為2(兩個Unicode值相減所得結果),表示s1大於s29 }View Code

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.