日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(JAVA)String类之比较方法

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (JAVA)String类之比较方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/* 字符串: 一、概述:1.字符串在JAVA中,使用""表示2.java.lang.String類3.只要寫""就是字符串對象。不需要new二、空參構造器new Sting();private final char value[];public String() {this.value = "".value;} 三、String類一個參數構造器:字節數組采用平臺(操作系統)默認字符集解碼數組--編碼表GBKbyte[] = {};將字節數組轉成字符串public String(byte bytes[]) {this(bytes, 0, bytes.length);} 四、String類兩個參數構造器:指定一個編碼表【I/O更新】五、String類三個參數構造器:字節數組,開始下標,獲取幾個 java.lang.String @Contract(pure = true) public String(@NotNull byte[] bytes,int offset,int length) 六、String類一個參數構造器:字符數組 public String(char value[]) {this.value = Arrays.copyOf(value, value.length);} 七、String類三個參數構造器:字符數組,開始下標,獲取幾個public String(char value[], int offset, int count) {if (offset < 0) {throw new StringIndexOutOfBoundsException(offset);}if (count <= 0) {if (count < 0) {throw new StringIndexOutOfBoundsException(count);}if (offset <= value.length) {this.value = "".value;return;}}// Note: offset or count might be near -1>>>1.if (offset > value.length - count) {throw new StringIndexOutOfBoundsException(offset + count);}this.value = Arrays.copyOfRange(value, offset, offset+count);} 八、String類三個參數構造器:整數數組,開始下標,獲取幾個(基本不用) 九、 String類三個參數構造器:字符串(基本不用) public String(String original) {this.value = original.value;this.hash = original.hash;}十、字符串一旦創建,就是常量,不能修改。 "abc"是對象---不能改變 s 是引用型變量 s 的指向對象可以改變十一、在堆中,如果 String s1 = new String('a','b','c'); String s2 = new String('a','b','c'); System.out.println(s1==s2);-------true但 String s3 = new String("abc"); String s4 = "abc"; System.out.println(s1==s2);-------Fals:創建對象不同String s5 = "efg"; System.out.println(s4==(s4+s5));---- f-------常量與變量相加,不知道結果,重新建立空間,比較的是內存地址 System.out.println(s4==("abc"+"def"));---- t------常量與常量相加,比較的是具體數值十二、比較方法equals():必須一模一樣才叫相等,是繼承Object類之后重寫的比較數組中的每一個字符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;} 十三、比較字符串是否相等,忽略大小寫:是StrinG自己的,非繼承重寫 boolean equalsIgnoreCase(String s)十四、判斷一個字符串中是否包含另一個字符串public boolean contains(CharSequence s) {return indexOf(s.toString()) > -1;} 十五、判斷字符串中是否包含空字符串public boolean isEmpty() {return value.length == 0;} 十六、判斷字符串是不是以另一個字符串開頭public boolean startsWith(String prefix) {return startsWith(prefix, 0);}public boolean startsWith(String prefix, int toffset) {char ta[] = value;int to = toffset;char pa[] = prefix.value;int po = 0;int pc = prefix.value.length;// Note: toffset might be near -1>>>1.if ((toffset < 0) || (toffset > value.length - pc)) {return false;}while (--pc >= 0) {if (ta[to++] != pa[po++]) {return false;}}return true;}判斷字符串是不是以另一個字符串結尾public boolean endsWith(String suffix) {return startsWith(suffix, value.length - suffix.value.length);}*/

1.空指針異常

?

public class StringDome {public static void main(String[] args) {method_1();}public static void method_1() {String s1 = "abcde";String s2 = new String("abcde");String s3 = null;String s4 = "ABCDE";String s5 = "ab";String s6 = "";System.out.println(s1.equals(s2));//System.out.println(s3.equals(null));System.out.println(s1.equalsIgnoreCase(s4));System.out.println(s1.contains(s5));System.out.println(s6.isEmpty());System.out.println(s1.startsWith("a"));System.out.println(s1.endsWith("d"));}

總結

以上是生活随笔為你收集整理的(JAVA)String类之比较方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。