Java API_String類

來源:互聯網
上載者:User

標籤:string+   api   ISE   class   []   dmi   sig   現在   number   


String類

1.String類的概述和構造方法以及相關方法

(1).String類概述
字串就是由多個字元組成的一串資料。也可以看成是一個字元數組。

(2).String類的構造方法
public String():空參構造
public String(byte[] bytes):把位元組數組轉化成為字串
public String(byte[] bytes,int index,int length):把位元組數組的一部分轉化成為字串
public String(chart[] value):把字元數組轉成字串
public String(chart[] value,int index,int count):把字元數組的一部分轉成字串
public String(String original):把字串常量值轉成字串


使用舉例:
public static void main(String[] args) throws CloneNotSupportedException {
char[] chs={‘a‘,‘b‘,‘c‘,‘d‘,‘e‘};
String str=new String(chs);
System.out.println(str);
}
//輸出結果:
abcde

(3).String類的面試題1
String s=new String("hello");和String s="hello";有什麼區別?
String s=new String("hello");
建立了倆個對象,一個是先建立了"hello"的字串常量,然後又建立了一個對象s(new 出來的東西位於堆記憶體),引用的是堆記憶體的地址
String s="hello"
只建立了一個對象,引用的是方法區裡面字串常量池裡面的地址。

(4).String類的面試題2
看程式寫結果並分析:
public static void main(String[] args) throws CloneNotSupportedException {
String str1="hello";
String str2="world";
String str3="helloworld";
System.out.println(str3==str1+str2);
System.out.println(str3.equals(str1+str2));
}
//輸出結果:
false
true

分析:
"==": 比較參考型別比較的是地址值是否相同
equals String類型重寫了equal()方法,比較的是內容是否相同

字串如果是變數相加,先開空間,然後再拼接
字串如果是常量相加,是先加,然後再常量池裡面找,如果有就直接返回,否則就建立。


(5).String類的判斷功能
boolean equals(Object obj):比較字串的內容是不是相同,區分大小寫
boolean equalsIgnoreCase(String str):比較字串的內容是否相同,忽略大小寫
boolean contains(String str):判斷字串中是否包含特定字串
boolean startWidth(String str):判斷字串是否以某個指定的字串開頭
boolean endsWidth(String str):判斷字串是否以某個指定的字串結尾
boolean isEmpty();判斷字串是否為空白

使用注意事項:
String s=""; //可以調用方法
String s=null; //不可以調用方法
字串內容為空白(存在對象但是沒有內容)和字串對象為空白(表示的是連對象都不存在)


(6).String類的擷取功能
int length():擷取字串的長度
char charAt(int index):擷取指定索引位置的字元
int indexOf(int ch):返回指定字元在此字串中第一次出現的索引(注意是字元)
int indexOf(String str):返回指定字串在此字串中第一次出現的索引(注意是字串)
int indexOf(int ch,int formIndex):返回指定字元在此字串中從指定位置後第一次出現處的索引
int indexOf(String str,int formIndex):返回指定字串在此字串中從指定位置後第一次出現處的索引
String subString(int start):從指定位置開始截取字串。預設到末尾
String subString(int start ,int end):從指定位置開始到指定位置結束截取字串。


(7).String類的基本使用舉例

A:變數擷取字串中的每一個字元
基本方法
如何擷取每一個字元
char chartAt(int index);
擷取有多少個字元
int length()

代碼:
public static void main(String[] args){
String str ="helloWorld";
for(int i=0;i<str.length();i++){
System.out.println("第"+i+"擷取的字元是:"+str.charAt(i));
}
}

//輸出結果:
第0擷取的字元是:h
第1擷取的字元是:e
第2擷取的字元是:l
第3擷取的字元是:l
第4擷取的字元是:o
第5擷取的字元是:W
第6擷取的字元是:o
第7擷取的字元是:r
第8擷取的字元是:l
第9擷取的字元是:d

B:統計字串中的大小寫、數字
使用基本方法
char chartAt(int index);
int length();

大小寫數字ASCII碼
0 48
A 65
a 97

//判斷使用代碼
if(ch>=‘0‘&&ch<=‘9‘){
numberCount++;
}
if(ch>=‘a‘&&ch<=‘z‘){
smallCount++;
}
if(ch>=‘A‘&&ch<=‘A‘){
bigCountCount++;
}

代碼實現:
public static void main(String[] args) {
//定義需要尋找的字串
String str="HeLLoWorld129090";
//定義三個統計不了
int bigCount=0;
int smallCount=0;
int numberCount=0;
//遍曆字串,擷取每一個字元
for(int i=0;i<str.length();i++){
char ch=str.charAt(i);
//判斷是否屬於哪一種類型
if(ch>=‘a‘&&ch<=‘z‘){
smallCount++;
}else if(ch>=‘A‘&&ch<=‘Z‘){
bigCount++;
}else if(ch>=‘0‘&&ch<=‘9‘){
numberCount++;
}
}
//輸出結果
System.out.println(str+":中大寫字母有:"+bigCount+"個---小寫字母有:"+smallCount+"個---數字有"+numberCount+"個");
}

輸出結果為:
HeLLoWorld129090:中大寫字母有:4個---小寫字母有:6個---數字有6個


(8).字串的轉化功能
byte[] getBytes():把字串轉化為位元組數組
char[] toCharArray():把字串轉化成為字元數組
static String valueOf(char[] chs):把字元數組轉化成為字串
static String valueOf(int i):把int類型的資料轉成字串
注意:String類的valueOf方法可以把任意的類型的資料轉成字串
String toLowerCase():把字串轉化成為小寫
String toUpperCase():把字串轉化成為大寫
String concat(String str):把字串拼接


(9).String類的其他功能
替換功能
String replace(char old,char new):把舊字元轉化成為新字元
String replace(String old,String new):把舊的字串轉化成為新字串

去除字元的倆端空格
String trim()

按照字典順序比較倆個字串
int compareTo(String str)
int compareToIgnoreCase(String str)

使用舉例:
public static void main(String[] args) {
String str="helloWorld";
String str1=" abc ";
String str2=" abcd ";
System.out.println(str.replace("h", " "));
System.out.println(str.replace("hello", " "));
System.out.println(str1.compareTo(str2));
System.out.println(str1.trim());
System.out.println(str1.compareToIgnoreCase(str2));
}
//輸出結果為
elloWorld
World
-68
abc
-68

(10).字串的基本使用舉例2

A:字串反轉
功能結果:鍵盤輸入"abc",輸出結果為"cba"

設計分析:
a:鍵盤輸入一個字串
b:定義一個新的字串
c:倒著遍曆這一個字串
length()方法和charAt()結合
把字串轉化成為字元數組
d:用新的字串把每一個字元拼接起來
e:輸出新字串

代碼實現:
public class Test {
public static void main(String[] args) {
//鍵盤輸入
@SuppressWarnings("resource")
Scanner sn =new Scanner(System.in);
System.out.println("請輸入一個字串:");
String line=sn.nextLine();
String s=reverseString(line);
System.out.println("倒敘以後的結果是:"+s);
}

/**
*字串反轉
*@author Administrator
*@param String str:原始字串
*@return String str:結果字串
*
*/
public static String reverseString(String str){
//定義返回字串
String result="";
//把字串轉化成為字元數組
char[] chs=str.toCharArray();
//倒著遍曆,擷取每一個字串
for(int i=chs.length-1;i>=0;i--){
//使用新字串把每一個字元拼接起來
result +=chs[i];
}
return result;
}
}


B:統計一個字串中小字串出現的次數

代碼設計分析:
定義一個大字串
定義一個小字串

a:定義一個統計變數,並初始化值為0
b:現在大字串中尋找小字串中出現的第一次位置
如果說索引是-1,說明在大字串中沒有出現小字串
如果說索引不是-1,說明存在,統計變數++
c:把剛才的索引+小字串的長度作為開始位置截取上一次的大字串,返回一個新的字串,並把該字串的值重新賦值給大字串
d:返回到b過程

代碼實現
public class Test {
public static void main(String[] args) {
//定義大的字串
String maxString="javafjhfvsadvfkashjfvasjjavaalhsdfajavabs";
//定義小字串
String minString="java";
int count=getCount(maxString, minString);
System.out.println(minString+"出現的次數是:"+count);

}

/**
*統計大的字串中小字串出現的次數
*@author Administrator
*@param String str:原始的字串
*@return int :返回出現的次數
*
*/
public static int getCount(String maxString,String minString){
//定義一個統計變數
int count=0;
//先在大串中尋找小串第一次出現的位置,然後再複製
int index=maxString.indexOf(minString);
//如果說索引並不是-1,說明存在,統計變數++
while((index=maxString.indexOf(minString))!=-1){
count++;
maxString=maxString.substring(index+minString.length());
}
return count;
}
}
//測試結果
java出現的次數是:3

Java API_String類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.