java字符串String
前言
String類代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被實現為此類的實例。
字符串不變; 它們的值在創建后不能被更改。 字符串緩沖區支持可變字符串。 因為String對象是不可變的,它們可以被共享
1、構造器
參數是字節數組
bytes - 要解碼為字符的 byte
offset - 要解碼的第一個 byte 的索引(包括)
length - 要解碼的 byte 數
案例
byte[] b = {55,66,77,88,99}; String s = new String(b,2,3); System.out.println(s);//MXc參數是字符數組
value - 要解碼為字符數組
offset - 要解碼的第一個 byte 的索引(包括)
count - 要解碼的 字符數
案例
char[] b = {'a','b','c','d','e'}; String s = new String(b,2,3); System.out.println(s);//cde參數是int數組
String(int[] codePoints, int offset, int count)案例
int [] b = {55,66,77,88,99}; String s = new String(b,2,3); System.out.println(s);//MXc參數是對象
String(String original)//初始化新創建的對象 String所以它代表相同的字符序列的說法;換句話說,新創建的字符串是一個副本的參數字符串。 String(StringBuffer buffer)//分配一個新的字符串,該字符串包含當前包含在字符串緩沖區中的字符的序列的字符串。 String(StringBuilder builder)//分配一個新的字符串,該字符串包含當前包含在字符串生成器參數中的字符的序列。2、將對象換成字符串形式
//對象轉換 static String valueOf(Object obj)//將基本類型轉換成字符串形式,注意:沒有byte和short的轉換 static String valueOf(boolean b)//返回的 boolean參數的字符串表示形式。 static String valueOf(double d)//返回的 double參數的字符串表示形式。 static String valueOf(float f)//返回的 float參數的字符串表示形式。 static String valueOf(int i)//返回的 int參數的字符串表示形式。 static String valueOf(long l)//返回的 long參數的字符串表示形式。 static String valueOf(char c)//返回的 char參數的字符串表示形式。//將字符數組轉換成字符串形式 static String valueOf(char[] data)//返回的 char數組參數的字符串表示形式。 static String valueOf(char[] data, int offset, int count)//返回一個特定的子陣的 char數組參數的字符串表示形式。//將字符串轉換成數組 char[] toCharArray()將此字符串轉換為一個新的字符數組。byte[] getBytes() byte[] getBytes(Charset charset) byte[] getBytes(String charsetName)3、比較順序
區別大小寫:比較兩個字符串的字典。該比較基于字符串中各個字符的 Unicode 值
int compareTo(String anotherString)源碼
public int compareTo(String anotherString) {int len1 = value.length;//獲取當前字符的長度int len2 = anotherString.value.length;//獲取要比較字符長度int lim = Math.min(len1, len2);//返回最小的長度char v1[] = value;//把String的屬性char value[] 賦值到局部變量v1char v2[] = anotherString.value;int k = 0;//當前最小字符串長度大于0while (k < lim) {char c1 = v1[k];char c2 = v2[k];if (c1 != c2) {return c1 - c2;}k++;}//當前最小字符串長度等于0,那么返回的是字符串的長度return len1 - len2; }總結:如果存在最小字符串長度等于0,那么返回的是字符串的長度;否則比較的最開始不同字符Unicode 值的差值
情況1:存在最小字符串長度等于0。結果是字符串的長度,
//a的Unicode值是97 b的Unicode值98 String s = new String("abc"); String s1 = new String(""); int e = s.compareTo(s1); int d = 'a'; System.out.println(d);97 System.out.println(e);3情況2 : 不存在最小字符串長度等于0。比較的最開始不同字符Unicode 值的差值
String s = new String("abc"); String s1 = new String("b"); int e = s.compareTo(s1); int a = 'a'; int b = 'b'; System.out.println(a-b); -1 System.out.println(e); -1忽略大小寫的差異
int compareToIgnoreCase(String str)源碼
public int compare(String s1, String s2) {int n1 = s1.length();int n2 = s2.length();int min = Math.min(n1, n2);for (int i = 0; i < min; i++) {char c1 = s1.charAt(i);char c2 = s2.charAt(i);if (c1 != c2) {c1 = Character.toUpperCase(c1);c2 = Character.toUpperCase(c2);if (c1 != c2) {c1 = Character.toLowerCase(c1);c2 = Character.toLowerCase(c2);if (c1 != c2) {// No overflow because of numeric promotionreturn c1 - c2;}}}}return n1 - n2; }總結:如果存在最小字符串長度等于0,那么返回的是字符串的長度;否則比較的最開始不同字符Unicode 值的差值。注意這里的差值都是小寫化后的差值
4、是否包含
boolean contains(CharSequence s)//如果并且只有當此字符串包含指定的字符序列的字符串值,則返回真值。案例
String s = new String("sdfgh"); System.out.println(s.contains("df"));//true5、equals,hashCode
boolean equals(Object anObject) //將此字符串與指定的對象比較。當且僅當該參數不為 null,并且是與此對象表示相同字符序列的 String 對象時,結果才為 true boolean equalsIgnoreCase(String anotherString)//將此 String 與另一個 String 比較,不考慮大小寫源碼
public boolean equals(Object anObject) {if (this == anObject) {return true;}if (anObject instanceof String) {String anotherString = (String)anObject;int n = value.length;if (n == anotherString.value.length) {char v1[] = value;char v2[] = anotherString.value;int i = 0;while (n-- != 0) {if (v1[i] != v2[i])return false;i++;}return true;}}return false; }總結:
1、如果地址相同返回true,
2、如果地址不同且都是String類型,判斷每個字符的Unicode值,如果相同返回true
源碼
public int hashCode() {int h = hash;if (h == 0 && value.length > 0) {char val[] = value;for (int i = 0; i < value.length; i++) {h = 31 * h + val[i];}hash = h;}return h;} boolean contentEquals(CharSequence cs)比較這個字符串來指定 CharSequence。 boolean contentEquals(StringBuffer sb)比較這個字符串來指定 StringBuffer。contentEquals和equals區別
這兩個方法都可以用來比較String對象內容序列的異同,但也存在差異。
最大的差別就是String的equals方法只有在另一個對象是String的情況下才可能返回true,
而contentEquals只要求另一個對象是CharSequence或其子類的對象
6、區域比較
toffset -此字符串中的子區域的索引。
other -字符串參數。
ooffset -在字符串參數的分區起始索引。
len -比較的字符數。
案例
String str = "0123dnf2"; String str2 = "29dnf4567"; System.out.println(str.regionMatches(4, str2, 2, 3));//true7、格式化
底層是Formatter的format方法
//使用指定的語言環境、格式字符串和參數返回一個格式化字符串。 static String format(Locale l, String format, Object... args)//使用指定的格式字符串和參數返回格式化的字符串。 static String format(String format, Object... args)8、復制
static String copyValueOf(char[] data)//等于 valueOf(char[])。 static String copyValueOf(char[] data, int offset, int count)//等于 valueOf(char[], int, int)。srcBegin :字符串中要復制的第一個字符的索引。
srcEnd:字符串中要復制的最后一個字符之后的索引。(不包括)
dst : 目標數組。
dstBegin: 目標數組中的起始偏移量
案例
String str = "65577m"; char[] chararr = new char[4]; str.getChars(0,3, chararr,1); System.out.print(chararr);//<空格>655 System.out.print(Arrays.toString(chararr));//[ ,6,5,5]9、字符串中字符信息
int codePointAt(int index)返回指定索引字符的代碼點。 int codePointBefore(int index)//返回指定索引前一個字符的代碼點。案例
char [] b = {'7','B','M','X','c'}; String s = new String(b); System.out.println(s.charAt(0));‘7’ System.out.println(s.codePointAt(0));55 System.out.println(s.codePointAt(1));66 System.out.println(s.codePointBefore(2));66 System.out.println(s.codePointBefore(3)); 7 int codePointCount(int beginIndex, int endIndex)//指定文本范圍中 Unicode 代碼點的數量案例
char [] b = {'7','B','M','X','c'}; String s = new String(b); System.out.println(s. codePointCount(1,4));3 System.out.println(s. codePointCount(2,4));210、字符串前后綴判斷
boolean startsWith(String prefix)測試這個字符串是否以指定的前綴開始。 boolean startsWith(String prefix, int toffset) toffset - 在此字符串中開始查找的位置。 boolean endsWith(String suffix)測試此字符串是否以指定的后綴結束案例
String s = new String("dd.s"); System.out.println(s.endsWith("s")); //true11、尋找字符
char charAt(int index)//返回指定索引的 char值。int indexOf(int ch) //ch - 一個字符(Unicode 代碼點)。int indexOf(int ch, int fromIndex)//返回在指定字符的第一個發生的字符串中的索引,在指定的索引處開始搜索。 int indexOf(String str)//返回指數在這個字符串指定的子字符串中第一個出現的。 int indexOf(String str, int fromIndex)//返回此字符串的指定子字符串中第一個出現在索引中,從指定索引處開始。 int lastIndexOf(int ch)//返回在指定字符的最后一個發生的字符串內的索引。 int lastIndexOf(int ch, int fromIndex)//返回在指定字符的最后一個發生的字符串內的索引,在指定的索引處搜索向后開始。 int lastIndexOf(String str)//返回指數在這個字符串的指定子字符串中最后出現。 int lastIndexOf(String str, int fromIndex)//返回此字符串的指定子字符串中最后出現在索引,搜索后從指定索引處開始。案例
String str = "rgh45hj"; System.out.println(str.indexOf('4'));//3 System.out.println(str.indexOf("4"));//312、字符串替換
String replace(char oldChar, char newChar)//一個從此字符串派生的字符串,它將此字符串中的所有 oldChar 替代為 newChar。 String replace(CharSequence target, CharSequence replacement)//每個子串替換該字符串指定的文本替換序列靶序列匹配的文字。String replaceAll(String regex, String replacement)//使用給定的 replacement 替換此字符串所有匹配給定的正則表達式的子字符串。 String replaceFirst(String regex, String replacement)//代替這個字符串的替換,給 regular expression匹配第一個字符串。案例1
String str = "1234s77sss"; System.out.println(str.replace('s', 'a'));//1234a77aaa案例2
String str = "1234ss77sss"; System.out.println(str.replace("ss", "a"));//1234a77as案例3
String str = "1234ss123sss"; System.out.println(str.replaceAll("[12]", "#"));//##34ss##3sss案例4
String str = "1234ss123sss"; System.out.println(str.replaceFirst("[12]", "#"));//#234ss123sss13、字符串按正則分割
String[] split(String regex)//根據給定正則表達式的匹配拆分此字符串。 String[] split(String regex, int limit) //limit - 分割的次數案例
String str = "1234ss123sss"; System.out.println(Arrays.toString(str.split("\\d",3)));//[, , 34ss123sss]14、字符串截斷
String substring(int beginIndex)beginIndex - 起始索引(包括)。 String substring(int beginIndex, int endIndex)CharSequence subSequence(int beginIndex, int endIndex)endIndex - 結束索引(不包括)。案例
String str1 = "012345678"; System.out.println(str1.substring(1, 5));//1234String str2 = "0123dnf2"; System.out.println(str2.subSequence(2,5)); 23d15、大小寫轉換
String toLowerCase()將所有的角色在這 String以較低的情況下使用默認的區域設置規則。 String toLowerCase(Locale locale)將所有的角色在這 String以較低的情況下使用給定的 Locale規則。 String toUpperCase()將所有的角色在這 String上使用的默認區域設置規則。 String toUpperCase(Locale locale)將所有的角色在這 String大寫使用給定的 Locale規則。16、字符串插入
//返回一個由 CharSequence elements副本的新字符串連接在一起的一份指定的 delimiter。 static String join(CharSequence delimiter, CharSequence... elements)//返回一個由 CharSequence elements String加入新的副本,連同一份指定的 delimiter。 static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)案例
String [] str = {"a","b","c"}; System.out.println(String.join("-",str));a-b-c17、拼接字符串
String concat(String str)將指定字符串連接到此字符串的結尾。案例
String s = new String("s"); System.out.println(s.concat("d")); //sd18、前導和尾隨空格刪除
String trim()案例
String str = " sdfg "; System.out.println(str.trim()); sdfg19、正則匹配
boolean matches(String regex)//告訴是否這個字符串匹配給定 regular expression。總結
以上是生活随笔為你收集整理的java字符串String的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot的常用endpoin
- 下一篇: 保护卡自动改IP程序实现方法(根据现有保