- substring() 提取字符串挑庶,區(qū)間左開右閉。
第一種形式:String substring (int StartIndex)
第二種形式:String substring(int startIndex,int endIndex) 里面的數(shù)據(jù)分別是開始索引和終止位后一位的索引淘捡。
如果起始位和終止位相同則截取結(jié)果是null遂黍。
- **concat() **連接兩個字符
例:String s = ”Welcome to“原茅;
String t = s.concat("AnHui");
注意這兩個字符是直接連接的揣非,中間不會有空格。
- **replace() **替換
String Str = new String("Runoob");
System.out.println(Str.replace("o", "tt"));
System.out.println(Str.replace('u', 'D'));
結(jié)果:Runttttb
RDnoob
該函數(shù)將所有的符合條件的匹配字符都替代掉达址,需要 注意 的是 該函數(shù)蔑祟,替換項和被替換項如果是 字符 那都要是字符,如果是字符串苏携,那就都是字符串做瞪。而且當(dāng)替換項是字符串的時候,不需要關(guān)注替換項的位數(shù)和被替換項的位數(shù)是否相同
replaceAll()是一種基于表達(dá)規(guī)則的替換右冻,相比于 replace 的只能基于字符本身的替換要靈活多變装蓬。
但是 replaceAll 使用的是正則表達(dá)式。
replaceFirst()功能是替換第一項所找到的匹配項纱扭,同樣和replaceAll相同使用的是正則表達(dá)式牍帚。
- **trim() ** 去掉起始和結(jié)尾的空格
- toLowerCase() 字母轉(zhuǎn)化為小寫
- toUpperCase() 字母轉(zhuǎn)化為大寫
- charAt() 獲取字符串該位置的字符。
- **getChars() ** 截取多個字符
void getChars(int sourceStart,int sourceEnd,char target[],int targetStart)
sourceStart 指定了子串開始字符的下標(biāo)
sourceEnd 指定了子串結(jié)束后的下一個字符的下標(biāo)乳蛾。 區(qū)間左開右閉暗赶。
target 指定接收字符的數(shù)組
targetStart target中開始復(fù)制子串的下標(biāo)值
String b = "hahah";
char[] a = new char[5];
b.getChars(0, b.length(), a, 0);
System.out.println(a);
需要注意鄙币,接受數(shù)組的長度如果小于截取長度,系統(tǒng)會報錯蹂随。
-
getBytes()
替代getChars()的一種方法是將字符存儲在字節(jié)數(shù)組中十嘿,該方法即getBytes()
String s = "Hello!你好!";
byte[] bytes = s.getBytes();
for (byte a :bytes)
System.out.println(a);
//72 101 108 108 111 33 -28 -67 -96 -27 -91 -67 -17 -68 -127
- toCharArray()
String s = "Hello!你好岳锁!";
char[] a = s.toCharArray();
for (char b: a)
System.out.print(b + " ");
//H e l l o ! 你 好 绩衷!
-
indexOf()和lastIndexOf()
indexOf() 查找字符或者子串第一次出現(xiàn)的地方。
lastIndexOf() 查找字符或者子串是后一次出現(xiàn)的地方激率。 如果該字符或者字符串沒有出現(xiàn)咳燕,則返回值 - 1
- equals() 判定兩個字符串是否相同。不能忽略大小寫的區(qū)別乒躺。
對于忽略大小寫的比較方法招盲。需要再equals 之后 添上 IgnoreCase。
String a = "a";
String b = "A";
System.out.println(a.equalsIgnoreCase(b));
// ture
- split() 字符串分割
分割之后要用數(shù)組去 保存嘉冒。
String a = "asdf asf asf kj klj";
String[] b = a.split(" ");
for (String c : b)
System.out.print(c + " ");
//asdf asf asf kj klj
-
startsWith()和endsWith() 判斷是否是特定的字符開始曹货。
startsWith()方法決定是否以特定字符串開始endWith()方法決定是否以特定字符串結(jié)束
String a = "abcdefg"; System.out.println(a.startsWith("a",3));
String a = "abcdefg"; System.out.println(a.startsWith("a"));
這兩個方法既可以不加索引直接從頭開始,也可以添加索引從索引位置判斷讳推。
但是這種方法和charAt()有什么區(qū)別嗎控乾?
這種方法和charAt()的最大區(qū)別在于它可以直接判斷 字符串,對于單個字符的判斷 s 和 c 都可以完成娜遵,但是當(dāng)需要判斷的是字符串時,s 明顯要比 c 放邊上很多壤短。
- regionMatches() 方法用于檢測兩個字符串在一個區(qū)域內(nèi)是否相等设拟。
public boolean regionMatches(int toffset,
String other,
int ooffset,
int len)
或
public boolean regionMatches(boolean ignoreCase,
int toffset,
String other,
int ooffset,
int len)
- ignoreCase -- 如果為 true,則比較字符時忽略大小寫久脯。
- toffset -- 此字符串中子區(qū)域的起始偏移量纳胧。
- other -- 字符串參數(shù)。
- ooffset -- 字符串參數(shù)中子區(qū)域的起始偏移量帘撰。
- len -- 要比較的字符數(shù)跑慕。
public class Test {
public static void main(String args[]) {
String Str1 = new String("www.runoob.com");
String Str2 = new String("runoob");
String Str3 = new String("RUNOOB");
System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(4, Str2, 0, 5));
System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(4, Str3, 0, 5));
System.out.print("返回值 :" );
System.out.println(Str1.regionMatches(true, 4, Str3, 0, 5));
}
}
以上程序執(zhí)行結(jié)果為:
返回值 :true
返回值 :false
返回值 :true