Java中系統類String的常用方法

2021-08-14 16:52:35 字數 3318 閱讀 1240

字串是個常量也叫常量字串

只要對字串進行操作 都返回的是乙個新的字串(原字串不能更改)

例如:public

static

void main(string args)

系統類的常用方法:
根據索引獲取字串中的字元

(使用 charat方法)

public

static

void

main(string args)

public

static

fun1()

根據字元獲取字串中的位置

(使用indexof方法)

public

static

void

main(string args)

public

static

fun1()

傳入字元返回對應索引:

indexof(int ch)

從當前位置開始查詢:

indexof(int ch,int fromindex)

傳入字串查詢對應位置:

indexof(string str)

public

static

void

main(string args)

(使用concat方法)

public

static

void

fun3()

是否包含這個字串

(使用 contains方法)

public

static

void

main(string args)

public

static

void

fun4()

是否以這個字首開頭

(使用startswith方法)

public

static

void

main(string args)

public

static

void

fun5()

是否以這個字尾結尾

(使用endswith方法)

public

static

void

main(string args)

public

static

void

fun6()

(使用replace方法)

public

static

void

fun7()

(使用split方法)

//切割

public

static

void

fun8()

(//增強for迴圈 (foreach) 用來快速列印 快速遍歷

//冒號後 是你要 遍歷的容器

//冒號前 代表的是 容器中 每乙個元素

for (string string2 : strings) )

}

切割:split(regex);

(使用substring方法)

public

static

void

fun9()

從索引處 開始擷取 返回的是 從索引處往後的字串:

substring(beginindex)

從索引處 開始擷取 到 結束索引處

且 留頭 不留尾:

substring(beginindex, endindex)

(使用touppercase()轉大寫)

public

static

void

fun10()

(使用tolowercase()轉小寫)

public

static

void

fun11()

轉大寫:

touppercase()

轉小寫:

tolowercase()

(使用trim方法)

public

static

void

fun12()

去空格:

trim();

/*

* 字串相等時 返回0

* 兩個字串 長度相同時

* 一位一位進行ascii碼的比較

* 長度不相等 返回的是 長度的差值

*/(使用compareto方法)

public

static

void

fun13()

(使用 equalsignorecase())

public

static

void

fun14()

判斷兩個字串是否相等:

equalsignorecase()

字元陣列 轉 字串

public

static

void

fun15() ;

//轉字串

string string = new string(array);

system.out.println(array);

}

int 轉 字串

(使用valueof方法)

public

static

void

fun16();

//int 轉 字串

int num = 5;

string newstr = string.valueof(num);

system.out.println(newstr);

}

//字串 轉 字元陣列

(使用tochararray)

public

static

void

fun17()

}

//判斷字串是否為空

(使用 isempty方法)

public

static

void

fun18()

java系統類 String類詳解

1.系統類 string 學習系統常用方法 方法的使用 字串是個常量 常量字串 對字串進行操作 都返回是乙個新的字串 原字串不能更改 字串存在常量池中 字串本身沒有發生變換 而是引用位址發生了變化 相當於 指標重新指向 string str1 adcde str1 uvwxyz string1是乙個...

JAVA 系統類String 詳解

字串是個常量 常量字串 注意 對字串進行操作 都是返回乙個新的字串 原字串不能更改 字串存在於常量池中,字串本身 沒有發生變化 而是引用位址發生變化,相當於 指標重新指向 舉例 public class demo01 string 在jdk中的一些方法 應用舉例 public class demo0...

JAVA中String類的總結

類在 的使用中有著非常重要的作用,現在對於 類做乙個總結 提到 類就不得不提到 類,有兩種型別的字串,一種是建立後不需要改變的,稱為字串常量,類用於儲存字串常量。另一種是建立後需要對其進行改變的,稱為字串變數,用於儲存字串變數。先來看看 類 類有 種構造方法,都是根據提供的不同引數來構造。下面舉幾個...