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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java实用教程——常用实用类——String类(字符串类)

發布時間:2023/12/4 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java实用教程——常用实用类——String类(字符串类) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

JAVA把String類定義為final類(因此用戶不能擴展String類,即String類不可以有子類)
String對象可以用"+"進行并置運算
identityHashCode會返回對象的hashCode,而不管對象是否重寫了hashCode方法。

public class Example8_1 {public static void main(String args[]) {String hello = "你好";String testOne = "你"+"好"; //【代碼1】int address =System.identityHashCode("你好");System.out.printf("\"你好\"的引用:%x\n",address);address =System.identityHashCode(hello);System.out.printf("hello的引用:%x\n",address);address =System.identityHashCode(testOne);System.out.printf("testOne的引用:%x\n",address);System.out.println(hello == testOne); //輸出結果是trueSystem.out.println("你好" == testOne); //輸出結果是trueSystem.out.println("你好" == hello); //輸出結果是trueString you = "你";String hi = "好";String testTwo = you+hi; //【代碼2】address =System.identityHashCode("你");System.out.printf("\"你\"的引用:%x\n",address);address =System.identityHashCode("好");System.out.printf("\"好\"的引用:%x\n",address);address =System.identityHashCode(testTwo);System.out.printf("testTwo的引用:%x\n",address);System.out.println(hello == testTwo); //輸出結果是false}}


String類中的方法:
1.public int length():用來獲取一個String對象的字符序列

int number=s1.length();

2.public boolean equals(String s)用于比較當前String 對象的字符序列是否與s指定的String對象的字符序列相同

System.out.println(s1.equals(s2));

3.public boolean startsWith(String s)
判斷當前String對象的字符串序列的前綴是否是參數指定的String對象s的字符序列
public boolean endsWith(String s)

4.public int compareTo(String s)
按照字典序與參數指定的String對象S的字符串序列比較大小

單詞的排序
用Array類自帶的方法 Arrays.sort(b);
b里面有所有的單詞組成的大字符串

package lg;import java.util.*; public class Example8_3 {public static void main(String args[]) {String [] a={"melon","apple","pear","banana"};String [] b={"西瓜","蘋果","梨","香蕉"};System.out.println("使用SortString類的方法按字典序排列數組a:");for(int i=0;i<a.length;i++) {System.out.print(" "+a[i]);}System.out.println(" ");SortString.sort(a);for(int i=0;i<a.length;i++) {System.out.print(" "+a[i]);}System.out.println("");System.out.println("使用類庫中的Arrays類,按字典序排列數組b:"); Arrays.sort(b);for(int i=0;i<b.length;i++) {System.out.print(" "+b[i]);}} } public class SortString {public static void sort(String a[]) {int count=0; for(int i=0;i<a.length-1;i++) {int saveMinIndex = i;for(int j=i+1;j<a.length;j++) { if(a[j].compareTo(a[saveMinIndex])<0) {saveMinIndex = j;}} String temp=a[i];a[i]=a[saveMinIndex];a[saveMinIndex]=temp;}} }


5.public boolean contains(String s)
判斷當前String的對象字符序列是否包含參數S的字符序列
6.public int indexOf(String s)
從當前String對象的字符序列的0索引位置開始檢索首次出現str的字符序列的位置,并返回位置
public int lastIndexOf(String s)
從當前String對象的字符序列的0索引位置開始檢索最后一次出現str的字符序列的位置,并返回位置

找不到返回-1

String tom = "I am a good cat"; tom.indexOf("a");//值是2 tom.lastIndexOf("a");//值是13

7.public String substring(int startpoint)
substring(int startpoint)方法是字符串對象調用該方法獲得一個當前字符串的子串,該子串是從當前字符串的startpoint處截取到最后所得的字符串(注:字符串的起始位置是從0開始的,截取的時候startpoint位置的字符也被截取)

substring(int start,int end)
方法是獲取一個當前字符串的子串,該子串是通過復制當前字符串start到end-1位置上的字符串(注:字符串的起始位置是從0開始)
8。public char charAt(int index)
charAt()方法是用來輸出一個字符串中的單個字符,例如:
String s = “hello world”;
system.out.println(s.charAt(1));
輸出的結果就為e

總結

以上是生活随笔為你收集整理的java实用教程——常用实用类——String类(字符串类)的全部內容,希望文章能夠幫你解決所遇到的問題。

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