生活随笔
收集整理的這篇文章主要介紹了
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
= "你" + "好" ; 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
) ; System . out
. println ( "你好" == testOne
) ; System . out
. println ( "你好" == hello
) ; String you
= "你" ; String hi
= "好" ; String testTwo
= you
+ hi
; 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
) ; } }
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" ) ;
tom
. lastIndexOf ( "a" ) ;
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类(字符串类) 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。