生活随笔
收集整理的這篇文章主要介紹了
(JAVA)String类之比较方法(2)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
String類:
獲取字符串的內容
一、獲取字符串長度public int length() {return value.length;}length 與 lenght()的區別:前面是屬性,不需要傳參數后面是方法
二、指定下角標,返回固定位置的對應字符串public char charAt(int index) {if ((index < 0) || (index >= value.length)) {throw new StringIndexOutOfBoundsException(index);}return value[index];}
三、返回一個字符在字符串中第一次出現的位置
public int indexOf(String str) {return indexOf(str, 0);}
四、返回一個字符在指定位置后,出現的位置public int indexOf(String str, int fromIndex) {return indexOf(value, 0, value.length,str.value, 0, str.value.length, fromIndex);}
五、返回一個字符(字符串)在另一個字符串中最后一次出現的位置public int lastIndexOf(String str) {return lastIndexOf(str, value.length);}
六、獲取字符串的一部分
String substring(int beginIndex,int endIndex);public String substring(int beginIndex, int endIndex) {if (beginIndex < 0) {throw new StringIndexOutOfBoundsException(beginIndex);}if (endIndex > value.length) {throw new StringIndexOutOfBoundsException(endIndex);}int subLen = endIndex - beginIndex;if (subLen < 0) {throw new StringIndexOutOfBoundsException(subLen);}return ((beginIndex == 0) && (endIndex == value.length)) ? this: new String(value, beginIndex, subLen);}
七、轉換
1.將字符串轉換為字節數組public byte[] getBytes(Charset charset) {if (charset == null) throw new NullPointerException();return StringCoding.encode(charset, value, 0, value.length);}
2.將字符串全部轉成小寫public String toLowerCase(Locale locale) {if (locale == null) {throw new NullPointerException();}int firstUpper;final int len = value.length;Now check if there are any characters that need to be changed.scan: {for (firstUpper = 0 ; firstUpper < len; ) {char c = value[firstUpper];if ((c >= Character.MIN_HIGH_SURROGATE)&& (c <= Character.MAX_HIGH_SURROGATE)) {int supplChar = codePointAt(firstUpper);if (supplChar != Character.toLowerCase(supplChar)) {break scan;}firstUpper += Character.charCount(supplChar);} else {if (c != Character.toLowerCase(c)) {break scan;}firstUpper++;}}return this;}3.將字符串全部轉為大寫String.toUpperCase(Locale.ROOT);4.切割,將字符串切割為指定要求的字符串數組,參數為正則表達式String[] split5,替換String replase(char oldchar.char newchar);String replase(String oldString.String newString);6.去掉字符串中兩端空格去掉:String trim()7.兩個字符串的字典形式比較int compareTo(String s)
import java.nio.charset.StandardCharsets;
import java.util.Locale;public class getStringDome {public static void main(String[] args){methon_1();methon_2();methon_3();menth_4();methon_5();}public static void methon_1(){String a = "asdfhgvscasdf";System.out.println(a.length());System.out.println(a.charAt(3));System.out.println(a.indexOf("s"));System.out.println(a.indexOf("s",2));System.out.println(a.lastIndexOf("s"));System.out.println(a.lastIndexOf("s",4));System.out.println(a.substring(2,5));//遍歷字符串
// for (int x = 0;x<a.length();x++){
// System.out.println(a.charAt(x));
// }}public static void methon_2(){byte[] bytes="張三".getBytes(StandardCharsets.UTF_8);for (int x = 0;x < bytes.length;x++){System.out.println(bytes[x]);}byte[] c = {-27,-68,-96};String m = new String(c);System.out.println(m);}public static void methon_3(){String s1 = "AJDSKVvdknvv";s1 = s1.toLowerCase(Locale.ROOT);System.out.println(s1);String s2;s2 = s1.toUpperCase(Locale.ROOT);System.out.println(s2);}public static void menth_4(){String a ="asdkvjbwiejfhljzcnvndj";String[] a1 = a.split("j");for (int x = 0;x <a1.length;x++){System.out.println(a1[x]);}System.out.println("==========================");String b = "192.168.0.1";String[] b1 =b.split("\\.");for (int x = 0;x <a1.length;x++){System.out.println(b1[x]);}System.out.println("==========================");}public static void methon_5(){String c = "zxcvb" ;String c1 = "asdf";int c2 = c.compareTo(c1);System.out.println(c2);}
總結
1. Scanner 了解接收鍵盤輸入
? ?以后使用IO接收 ?Scanner(System.in)
2. 字符串 -- 熟練
?2.1 定義方式,兩種
? ?|-- 直接定義 String s = ""
? ?|-- new方法 ?String s = new String(""),不推薦使用?
? ?|-- 定義方法區別,前者一個對象,后者兩個對象
?2.2 字符串的底層結構--特性
? ?|-- private ?final char[] value 字符串就是一個字符數組
? ?|-- String字符串,是一個常量,不可以改變
? ?|-- 字符串面試題。引用類型中,數組,類類型,接口,都會改變,唯一就String不變
?2.3 構造器
? ?|-- 字節數組變成字符串, new 傳遞字節數組,查詢編碼表 byte[] = {};
? ?|-- 字符數組變成字符串, new 傳遞字符數組,不查詢編碼表 char[] = {}
?
?2.4 判斷方法
? ?|-- equals 重寫Object方法,比較字符串是不是完全相等
? ?|-- equalsIgnoreCase 比較字符串是不是完全相等,忽略大小寫
? ?|-- contains 判斷一個字符串是不是包含另一個字符串
? ?|-- isEmpty 判斷是不是空串
? ?|-- startsWith 判斷一個字符串是不是以另一個字符串開頭
? ?|-- endsWith 判斷一個字符串是不是以另一個字符串結尾
?2.5 獲取方法
? ?|-- charAt 指定索引返回單個字符
? ?|-- indexOf 指定字符,返回第一次出現的索引
? ?|-- lastIndexOf 指定字符,返回最后一次出現的索引
? ?|-- length 返回字符串中字符個數
? ?|-- substring 獲取字符串一部分,包含頭,不包含尾
?2.6 轉換方法
? ?|-- getBytes 查詢編碼表,將字符串轉成字節數組
? ?|-- toCharArray 不查詢編碼表,將字符串轉成字符數組
? ?|-- toUpperCase 字符串轉成大寫
? ?|-- toLowerCase 字符串轉成小寫
?2.7 其他方法
? ?|-- split 切割字符串
? ?|-- replace 替換字符串
? ?|-- trim 去掉兩端空格
? ?|-- compareTo 字典順序比較字符串,又成為自然順序
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的(JAVA)String类之比较方法(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。