String简述
String:
1.字符串類,java中的字符串文字都被視為字符串類的實例;
2.字符串對象被賦值后不能更改,是不可變的;
3.字符串使用+進行拼接操作,實際上是使用的StringBuilder或StringBuffer的append方法進行的,會創建對應的新對象;
4.從JDK1.0版本開始有的該類;
構造方法:
String():創建一個空字符序列;
String(byte[] bytes,String charsetName):使用指定的字符編碼,按照byte數組中的元素創建一個新的字符序列;
String(char[] value):使用字符數組中的元素創建新的字符序列;
String(String str):使用已存在的字符序列,創建一個一樣內容的字符序列,相當于是已存在字符序列的副本;
String(StringBuffer buffer):使用StringBuffer對象創建對應的字符序列;
String(StringBuilder builder):使用StringBuilder對象創建對應的字符序列;
方法:
length():獲取字符串的長度;
isEmpty():判斷字符串是否為空,當且僅當字符串的長度為0時,才返回true;
charAt(int index):獲取字符串指定索引處的字符內容,如果索引值超出了字符串的長度-1,會報StringIndexOutOfBoundsException異常;
getChars(int srcBegin,int srcEnd,char[] chars,int destBegin):將字符串中指定索引范圍內的字符拷貝到字符數組中,從字符數組的指定索引處開始拷貝;
srcBegin:字符串中字符的起始索引;
srcEnd:字符串中字符的終止索引,起始這個位置的字符不進行拷貝,只拷貝到srcEnd-1的位置;
chars:目標字符數組;
destBegin:目標字符數組的起始索引;
getBytes();獲取字符串在當前系統的默認編碼格式下的byte數組;
getBytes(String charsetName):根據指定的編碼格式獲取字符串的byte數組;
equals(Object object):判斷字符串是否相等,當且僅當參數不為null,并且是一個String類型的對象,并且具有相同的字符序列才返回true;
contentEquals(StringBuffer buffer):判斷字符串與buffer對象的字符序列是否相等;
equalsIgnoreCase(String str):不區分大小寫的情況下判斷兩個字符串的字符序列是否相等;
compareTo(String str):判斷兩個字符串的字符系列中對應字符的Unicode碼是否相等;
compareToIgnoreCase(String str):不區分大小寫的情況下,判斷兩個字符串的字符系列中對應字符的Unicode碼是否相等;
startsWith(String prefix):判斷字符串是否是以指定的前綴開頭的;
startsWith(String?prefix, int index):從字符串的指定索引開始,判斷后面的子字符串是否是以指定的前綴開頭的;
endsWith(String suffix):判斷字符串是否是以指定的后綴結尾的;
hashCode():獲取字符串的哈希碼值;
endsWith(String suffix):判斷字符串是否是以指定的后綴結尾的;
hashCode():獲取字符串的哈希碼值;
indexOf(int ch):獲取指定的字符在字符串中第一次出現的索引;
indexOf(String?str):回去指定的子字符串在當前字符串中第一次出現的索引;
indexOf(int ch,int fromIndex):從字符串指定的索引開始,獲取字符在字符串中第一次出現的索引;
indexOf(String subString,int fromIndex):從指定的索引開始,獲取子字符串在該字符串中第一次出現的索引;
lastIndexOf(int ch):獲取指定的字符在字符串中最后一次出現的索引;
lastIndexOf(int ch,int fromIndex):從字符串中的指定索引處反向查找字符最后一次出現的索引;
lastIndexOf(String subString,int fromIndex):從字符串指定的索引處反向查找子字符串最后一次出現的索引;
substring(int fromIndex):獲取字符串從指定的索引處到字符串結尾的子字符串,索引的值為0到字符串的長度處(不是字符串的長度-1處,此時,截取的子字符串為空字符串);
substring(int fromIndex,int endIndex):獲取字符串指定的索引范圍內(含頭不含尾)的子字符串;
concat(String str):進行字符串拼接,返回一個新字符串;
replace(char oldChar,char newChar):用新字符替換字符串中指定的字符;
replace(CharSequence target,CharSequence replacement):用新的字符序列替換字符串中指定的字符序列;
matches(String regex):判斷字符串是否匹配正則表達式;
split(String regex):使用符合正則表達式的子字符串分割字符串,返回值是一個字符串數組;
toUpperCase():將字符串中的字符轉換成對應的大寫;
toLowerCase():將字符串中的字符轉換成對應的小寫;
trim():去除字符串前后的空格,返回一個新字符串;
toCharArray():將字符串轉換成字符數組;
?
以下為靜態方法,可以使用String.方法名()直接調用:
valueOf(Object object):將對象轉換成對應的字符串形式;
valueOf(int i):將int類型的數據轉換成對應的字符串形式,同比,boolean、char、double、float、long類型的數據也可以進行相同轉化;
valueOf(char[] chars):使用字符數組中的元素創建一個新的字符串;
valueOf(char[] chars,int fromIndex,int count):從字符數組的指定索引處復制指定長度的元素創建一個新字符串;
copyValueOf(char[] chars):等同于valueOf(char[] chars);
copyValueOf(char[] chars,int fromIndex,int count):等同于valueOf(char[] chars,int fromIndex,int count);
?
練習代碼:
package com.yg.study;import java.io.UnsupportedEncodingException;public class StringStudy {public static void main(String[] args) {String str="abcabc123@#$";//獲取字符串的長度int len=str.length();System.out.println(len);//判斷字符串是否為空,當且僅當字符串的長度等于0的時候,才返回trueboolean isEmpty=str.isEmpty();System.out.println(isEmpty);//返回指定索引處的字符char c=str.charAt(3);System.out.println(c);//System.out.println(str.charAt(9));//會報StringIndexOutOfBoundsException異常//獲取指定索引范圍內的字符,返回一個字符數組char[] chars= new char[str.length()];str.getChars(0, 5, chars, 3);System.out.println(chars);//根據平臺默認的字符集獲取字符串對應的byte類型的數組byte[] bytes=str.getBytes();for (byte b : bytes) {System.out.println(b);}//根據指定的字符集回去字符串對應的byte類型的數組try {byte[] bytes2=str.getBytes("utf-8");for (byte b : bytes2) {System.out.println(b);}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch block e.printStackTrace();}//判斷字符串是否相等,當且僅當參數不為null,并且是一個String類型的對象,并且具有相同的字符序列才返回trueboolean equalFlag=str.equals(new String("abc123@#$"));System.out.println(equalFlag);//判斷內容相等boolean contentFlag=str.contentEquals(new StringBuffer("abc123@#$"));System.out.println(contentFlag);//不區分大小寫判斷兩個字符串相等boolean ignoreFlag=str.equalsIgnoreCase("AbC123@#$");System.out.println(ignoreFlag);//比較兩個字符串的大小int result=str.compareTo("ABC123@#$");System.out.println(result);//不區分大小寫比較兩個字符串的大小int result2=str.compareToIgnoreCase("ABC123@#$");System.out.println(result2);//判斷字符串是否以指定的前綴開始boolean startFlag=str.startsWith("abc");System.out.println(startFlag);//判斷字符串在指定的索引處是否以指定的前綴開始boolean startFlag2=str.startsWith("123", 3);System.out.println(startFlag2);//判斷字符串是否以指定的后綴結尾boolean endFlag=str.endsWith("@#$%");System.out.println(endFlag);//獲取字符串的哈希碼int hashCode=str.hashCode();System.out.println(hashCode);//獲取指定字符在字符串中第一次出現的索引int index=str.indexOf(97);System.out.println(index);//獲取指定的字符串在當前字符串中第一次出現的索引int index2=str.indexOf("123");System.out.println(index2);//從指定的索引開始查找特定的字符第一次出現的索引int index3=str.indexOf(98, 2);System.out.println(index3);//從指定的索引開始查找指定的字符串在當前的字符串中第一次出現的索引int index4=str.indexOf("abc",1);System.out.println(index4);//查找指定的字符在字符串中最后一次出現的索引int index5=str.lastIndexOf(98);System.out.println(index5);//從指定的索引開始反向查找指定的字符在字符串中最后一次出現的索引int index6=str.lastIndexOf(99, 4);System.out.println(index6);//從指定的索引開始反向搜索指定的字符串在當前字符串中最后一次出現的索引int index7=str.lastIndexOf("abc", 1);System.out.println(index7);//從指定的索引開始截取子字符串,包含該索引處,范圍為0到字符串的長度(此時為空字符串)String substr=str.substring(5);System.out.println(substr);//從指定的索引開始到指定的索引結束截取子字符串,含頭不含尾String substr2=str.substring(5, 8);System.out.println(substr2);//字符串的拼接String newString=str.concat("啦啦啦");System.out.println(newString);//字符串字符替換String newString2=str.replace('@','^');System.out.println(newString2);//字符串中的子字符串替換String newString3=str.replace("123", "666");System.out.println(newString3);//字符串匹配正則表達式boolean matchFLag=str.matches("[a-z0-9@#$]+");System.out.println(matchFLag);//分割字符串String[] strs=str.split("c");for (String string : strs) {System.out.println(string);}//字符串轉換成對應的大寫String upperString="中國abc".toUpperCase();System.out.println(upperString);//字符串轉換成對應的小寫String lowerString="aBC測試".toLowerCase();System.out.println(lowerString);//去除字符串前后的空格String trimString=" 測試 abc 哈哈 ".trim();System.out.println(trimString);//將字符串轉換成字符數組char[] chars2=str.toCharArray();for (char d : chars2) {System.out.println(d);}//將其他內容轉換成字符串String objString=String.valueOf(new Object());System.out.println(objString);String intString=String.valueOf(1181);System.out.println(intString);//相當于String.copyValueOf(char[] c)String charArrayString=String.valueOf(new char[] {'a','p','q','g'});System.out.println(charArrayString);//從字符數組的指定索引開始復制指定長度的元素創建一個新數組String charArrayString2=String.valueOf(new char[] {'a','p','q','g'},2,2); System.out.println(charArrayString2);//從指定的下標開始拷貝指定長度的字符數組生成新的字符串,如下從下標3開始,拷貝4個字符生成字符串String charArrayString3=String.copyValueOf(new char[] {'a','@','b','#','%','c','d'}, 3, 4);System.out.println(charArrayString3);String charArrayString4=String.copyValueOf(new char[] {'a','@','b','#','%','c','d'});System.out.println(charArrayString4);}}?
轉載于:https://www.cnblogs.com/antusheng/p/10346435.html
總結
- 上一篇: Maya mayapy.exe 安装 C
- 下一篇: 2019.02.11 bzoj4818: