该用户从未签到
|
String : 字符串类型
# D0 T8 R1 q3 P) a
+ C( M8 D1 }. N0 ]6 g: k+ R一、构造函数6 d" h9 V1 l g# t) V6 ?' y
String(byte[ ] bytes):通过byte数组构造字符串对象。 {; B9 X) w( X/ ?; a
String(char[ ] value):通过char数组构造字符串对象。
- k8 Q- x- z4 M9 ~$ T6 S% ]8 W String(Sting original):构造一个original的副本。即:拷贝一个original。, g# a. J! Z* `* z; }$ `
String(StringBuffer buffer):通过StringBuffer数组构造字符串对象。 1 y7 ?4 f w2 [, V
例如:
3 [) o" L7 a8 F byte[] b = {'a','b','c','d','e','f','g','h','i','j'};
# ]) f9 p0 t8 C3 _$ D char[] c = {'0','1','2','3','4','5','6','7','8','9'};
6 d! b7 o; R3 l F* `- a String sb = new String(b); //abcdefghij
; P4 Y- V! u7 d* I. _. z0 j String sb_sub = new String(b,3,2); //de% ]$ w3 f+ J& }0 M& w3 c6 E, V
String sc = new String(c); //0123456789
+ N7 @3 i- j( `; s1 E String sc_sub = new String(c,3,2); //34 v9 J# s, F2 e2 }. k" V" ^/ h& e
String sb_copy = new String(sb); //abcdefghij
" b# r _" w9 U1 w System.out.println("sb:"+sb);
0 p: r3 M' C& D6 q1 ^, c System.out.println("sb_sub:"+sb_sub);% d* d% t, l2 b- c3 i+ B4 a# i
System.out.println("sc:"+sc);) |7 U0 H; r' ?" ^. H$ \' l
System.out.println("sc_sub:"+sc_sub);+ [% `5 @3 Y* ^6 I
System.out.println("sb_copy:"+sb_copy);
8 ~0 ~( R8 m/ W 输出结果:sb:abcdefghij: I: r5 g$ h, a* ~* G
sb_sub:de1 {6 F1 g7 y+ F
sc:0123456789
, y) t6 f' S5 [; H' P# _ sc_sub:349 w8 w4 y4 v- v
sb_copy:abcdefghij7 t3 h8 [ L5 {5 F! t
+ l( ]" Q- r T) B二、方法:) x8 o6 m5 O5 u, z
" G, k/ K! a, P/ b7 G
说明:①、所有方法均为public。1 u& Q- B8 I: a; h. Z k
②、书写格式: [修饰符] <返回类型><方法名([参数列表])>
. D5 t3 ~9 i$ f% r- @8 y [5 w5 d- v1 c- s9 d% q! N
例如:static int parseInt(String s)$ D) G. E9 I$ e; w1 [0 D; u4 ~
表示此方法(parseInt)为类方法(static),返回类型为(int),方法所需要为String类型。
+ [3 P9 J L$ F1 x8 y/ N8 o% K* o6 ~! Y; ]* d
1. char charAt(int index) :取字符串中的某一个字符,其中的参数index指的是字符串中序数。字符串的序数从0开始到length()-1 。
( @/ k# t) |" C0 t& ?2 {; K& h0 Q2 {0 \! E2 U! ^
% y! }8 p3 H6 y; \9 A8 }5 O: J
4 H+ X# Z9 `8 ?( _, V3 h6 B
例如:String s = new String("abcdefghijklmnopqrstuvwxyz");6 y- m5 B+ c$ s; E+ K$ y) O$ X
System.out.println("s.charAt(5): " + s.charAt(5) );# _8 w9 n! N" N, P
结果为: s.charAt(5): f; ] t7 I( G0 |; b( h& _
2. int compareTo(String anotherString) :当前String对象与anotherString比较。相等关系返回0;不相等时,从两个字符串第0个字符开始比较,返回第一个不相等的字符差,另一种情况,较长字符串的前面部分恰巧是较短的字符串,返回它们的长度差。
6 H+ w3 |1 U( k( J6 V) H/ D! a- l3. int compareTo(Object o) :如果o是String对象,和2的功能一样;否则抛出ClassCastException异常。" K5 ^! k9 w1 f
例如:String s1 = new String("abcdefghijklmn");
) k/ \: P: }/ T; L, \! V String s2 = new String("abcdefghij");
! E8 [- g7 S% z; J0 Z! G& v5 s String s3 = new String("abcdefghijalmn");
8 z3 U5 v. d6 `& H7 [5 o4 t System.out.println("s1.compareTo(s2): " + s1.compareTo(s2) ); //返回长度差
0 B( }" J' A! n* ^( O! A, x System.out.println("s1.compareTo(s3): " + s1.compareTo(s3) ); //返回'k'-'a'的差 H. Z0 B7 B5 S0 E
结果为:s1.compareTo(s2): 4/ z2 p) q" i {3 h( _. v) Q
s1.compareTo(s3): 10
2 b3 e, |% z; l7 A# ?1 [7 {4. String concat(String str) :将该String对象与str连接在一起。
+ E) J7 S' Z/ d: _; f6 b5. boolean contentEquals(StringBuffer sb) :将该String对象与StringBuffer对象sb进行比较。* X% x# x: B, X
6. static String copyValueOf(char[] data) :4 b# Y6 B2 w* u% i
7. static String copyValueOf(char[] data, int offset, int count) :这两个方法将char数组转换成String,与其中一个构造函数类似。9 M( h/ m. @# l. q/ {% s# p
8. boolean endsWith(String suffix) :该String对象是否以suffix结尾。8 E! Z4 k. W* B0 Z9 K( }2 d+ R
例如:String s1 = new String("abcdefghij");/ E3 a- f% P" h2 V3 _! S/ }
String s2 = new String("ghij");
2 M0 E% g% i) r) |% g System.out.println("s1.endsWith(s2): " + s1.endsWith(s2) );/ w" J! n) m" C9 F' J8 @
结果为:s1.endsWith(s2): true- ^. u7 \$ _1 d
9. boolean equals(Object anObject) :当anObject不为空并且与当前String对象一样,返回true;否则,返回false。
3 V' F1 a# x$ n k. g! B, w0 R10. byte[] getBytes() :将该String对象转换成byte数组。
! {! N- m. w6 ?! j11. void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) :该方法将字符串拷贝到字符数组中。其中,srcBegin为拷贝的起始位置、srcEnd为拷贝的结束位置、字符串数值dst为目标字符数组、dstBegin为目标字符数组的拷贝起始位置。8 q+ E0 Q+ X, r7 Q6 P( @: r& h
例如:char[] s1 = {'I',' ','l','o','v','e',' ','h','e','r','!'};//s1=I love her!
; n2 r# i k* @% B String s2 = new String("you!"); s2.getChars(0,3,s1,7); //s1=I love you! R6 e8 R) p0 N( |4 R" F/ _% R/ x
System.out.println( s1 );* C9 t" k1 c( r1 L4 Z/ a
结果为:I love you!$ }7 g; b2 R) q4 c; u# M
12. int hashCode() :返回当前字符的哈希表码。
' w5 |8 V. x% j0 e, p# o13. int indexOf(int ch) :只找第一个匹配字符位置。
) v f* K5 S3 I, b14. int indexOf(int ch, int fromIndex) :从fromIndex开始找第一个匹配字符位置。7 t1 p2 h8 a, D) W
15. int indexOf(String str) :只找第一个匹配字符串位置。
* l7 U0 r$ k/ j! r16. int indexOf(String str, int fromIndex) :从fromIndex开始找第一个匹配字符串位置。
" W# I, J: i* i0 J5 `* y/ A 例如:String s = new String("write once, run anywhere!");0 T3 Q$ J* t7 |9 S; s
String ss = new String("run");. W7 j( N2 [9 p
System.out.println("s.indexOf('r'): " + s.indexOf('r') );0 e3 A p7 C9 f' ~2 J x
System.out.println("s.indexOf('r',2): " + s.indexOf('r',2) );9 u* v6 A; B4 t5 f7 T
System.out.println("s.indexOf(ss): " + s.indexOf(ss) );9 ^3 f4 ]; o- i8 ^3 \# }* W N
结果为:s.indexOf('r'): 1
/ ]/ Y1 b+ G( j+ Z$ U s.indexOf('r',2): 121 b5 H: I7 V T4 f& i1 V1 j
s.indexOf(ss): 123 }5 S; N4 X5 ~* X) t2 I; z
17. int lastIndexOf(int ch)9 ~6 R/ s, |6 {( }+ c
18. int lastIndexOf(int ch, int fromIndex)1 k: a1 W. {5 k0 E. Q. \! t
19. int lastIndexOf(String str)
3 x0 G4 f* I7 w20. int lastIndexOf(String str, int fromIndex) 以上四个方法与13、14、15、16类似,不同的是:找最后一个匹配的内容。; V1 B% H' v; X* y
public class CompareToDemo {
, G7 A8 o! l. J1 c' a! q$ @9 K public static void main (String[] args) {4 n3 `5 k& ]* w9 S! w& J; r5 n
String s1 = new String("acbdebfg");
5 D7 `* W E7 b# g# G# ?
8 J1 g; C, g7 ]4 }( L& ^* i8 b System.out.println(s1.lastIndexOf((int)'b',7));
9 Q0 U4 ?* l& G* w+ H: p0 l$ M }
7 B0 h) H/ I; c1 }+ @9 z2 w}
# k' i* s( ~1 O8 ^运行结果:56 _; B8 \, N- N6 W* G
(其中fromIndex的参数为 7,是从字符串acbdebfg的最后一个字符g开始往前数的位数。既是从字符c开始匹配,寻找最后一个匹配b的位置。所以结果为 5)
0 Q. r- s7 J0 f+ m6 g7 ]& O' W
$ ~: a8 U3 {" f+ x! f21. int length() :返回当前字符串长度。
* Q& \7 d2 P9 @1 F: @' p5 H22. String replace(char oldChar, char newChar) :将字符号串中第一个oldChar替换成newChar。9 }$ g- B- Q( R5 z0 A3 b* E- P% |
23. boolean startsWith(String prefix) :该String对象是否以prefix开始。/ s6 E* W+ n; Q$ @2 M
24. boolean startsWith(String prefix, int toffset) :该String对象从toffset位置算起,是否以prefix开始。
4 Q6 @8 c) E& C9 D 例如:String s = new String("write once, run anywhere!");' O& r; Z: b% R- P7 S
String ss = new String("write");
- y1 g$ r( M) {4 J" A1 T String sss = new String("once");3 J+ M+ ?1 _- a+ T
System.out.println("s.startsWith(ss): " + s.startsWith(ss) );' H) k7 K$ U1 P
System.out.println("s.startsWith(sss,6): " + s.startsWith(sss,6) );9 b0 z [% ~ N; t- k/ P6 J8 k2 f
结果为:s.startsWith(ss): true4 I( _! c3 o' i% v- U w
s.startsWith(sss,6): true
( N" a2 d) j' k, E% a& o25. String substring(int beginIndex) :取从beginIndex位置开始到结束的子字符串。* Q7 D0 v9 E' C2 q
26.String substring(int beginIndex, int endIndex) :取从beginIndex位置开始到endIndex位置的子字符串。+ m8 ]) q% A; R
27. char[ ] toCharArray() :将该String对象转换成char数组。* G" `% Q- ~, L* i# p
28. String toLowerCase() :将字符串转换成小写。
8 h2 O$ G" ?0 W. t- n! x) v( j2 @* x29. String toUpperCase() :将字符串转换成大写。
# p2 F+ w# F# X 例如:String s = new String("java.lang.Class String");- @+ l6 h6 B! j4 u" A' L J
System.out.println("s.toUpperCase(): " + s.toUpperCase() );
# |# F ` t3 E4 X6 y+ t9 X System.out.println("s.toLowerCase(): " + s.toLowerCase() );
* ^" c8 K( V/ Z 结果为:s.toUpperCase(): JAVA.LANG.CLASS STRING
" F& q" {7 L( O- X+ J! g s.toLowerCase(): java.lang.class string
8 k* ?3 b g; {$ I7 s- G+ r30. static String valueOf(boolean b)
- @3 [ T, h6 }! J' X" v" @31. static String valueOf(char c)
0 n9 I: _# d7 L0 k32. static String valueOf(char[] data)
' }0 I3 Y- t l33. static String valueOf(char[] data, int offset, int count)
) t \. U7 }7 Q) t' F34. static String valueOf(double d)
! o( P7 G6 j. Z2 G! V- F; V) y35. static String valueOf(float f); r% ?! d0 ^. f- ~5 E
36. static String valueOf(int i)1 a2 u W" L* ]8 s
37. static String valueOf(long l)
& {1 p/ y9 u, S m38. static String valueOf(Object obj)
$ f/ n; a1 Z; E# w: x6 l" M 以上方法用于将各种不同类型转换成Java字符型。这些都是类方法。
! Y) `( s& y1 C* M
2 T2 e0 ~! d1 b- H8 U7 x, Q% f! Z7 b$ r7 `
2 c% X$ i& p' }. j f- N
! ^8 }) I; \; }" J4 X8 `# O) p" _0 t
Java中String类的常用方法:
- W& F' |2 M; y4 ~9 |: g( P
8 D c* r) z* ~. G% o% N1 Opublic char charAt(int index)2 V% E0 H) z" E) J
返回字符串中第index个字符;
8 |5 p" e2 G; v! y; Npublic int length()
! m* P! }- P( A# L返回字符串的长度;
& _. M a9 V" D rpublic int indexOf(String str). |) V+ q/ c( _" F
返回字符串中第一次出现str的位置;. a- z# P( Z1 n9 M& @
public int indexOf(String str,int fromIndex)) m' l! {/ k( x- {/ a2 @' s1 {
返回字符串从fromIndex开始第一次出现str的位置;
. H5 M1 z; d2 P6 L; _' E |4 fpublic boolean equalsIgnoreCase(String another)) }7 t- y2 I; z% q( i
比较字符串与another是否一样(忽略大小写);
: w$ U) T) C, ]5 ?3 ppublic String replace(char oldchar,char newChar)) M0 ~5 i4 S& i
在字符串中用newChar字符替换oldChar字符! U% t% j1 T* O/ a9 p7 J7 ^
public boolean startsWith(String prefix)) U7 o: }8 H, w( M9 y" W H
判断字符串是否以prefix字符串开头; Y) D/ m7 S/ Z3 F) S$ i
public boolean endsWith(String suffix)1 i( V) R/ Z# |
判断一个字符串是否以suffix字符串结尾;
4 r8 B5 m& n, ]: G% opublic String toUpperCase()
- H X4 W7 Z+ v7 Q2 L' V- ^% q返回一个字符串为该字符串的大写形式;& T2 Q1 X7 n5 c4 r ~
public String toLowerCase()
6 L# N6 C. k3 @# z, C0 Z! Y) ]返回一个字符串为该字符串的小写形式* _1 l; i: U3 C' L, I$ _- Q9 b
public String substring(int beginIndex)! G0 H( \( Q. j2 c* M
返回该字符串从beginIndex开始到结尾的子字符串;
" x2 J2 m) a n. f7 R1 R0 _public String substring(int beginIndex,int endIndex)
/ o8 }2 u1 H7 S# {+ A' G: c. u返回该字符串从beginIndex开始到endsIndex结尾的子字符串
- q% ~* {& a1 N! C- v6 a# ^public String trim()
) R) @7 r- b( Y O0 E: h6 T返回该字符串去掉开头和结尾空格后的字符串' T* g7 p! U0 w1 U( ]8 ]' N
public String[] split(String regex)" Y$ Q7 G6 u/ i, b
将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
0 L# H. V% k- ^. R6 g2 |& ?! g实例: * V0 X$ k" s$ S% b
public class SplitDemo{
" J% A* h8 U9 F' T8 U public static void main (String[] args) {
) `- X8 k) g2 P% d& k
8 j, ~4 S$ T" p. `; G6 |; e' _ String date = "2008/09/10";
4 U0 `' S% a2 P& p3 S0 ` String[ ] dateAfterSplit= new String[3];
" f- ?' e2 j2 V' |# S& V% I; ? dateAfterSplit=date.split("/"); //以“/”作为分隔符来分割date字符串,并把结果放入3个字符串中。
0 Y- ~; a9 y1 a3 R" [ s5 U& I' {- d! h, [6 f- t
for(int i=0;i<dateAfterSplit.length;i++)
1 p/ t. O/ m+ D! i; `0 m' d$ t" u System.out.print(dateAfterSplit+" ");' y5 a2 D9 ]8 |0 m! c
}$ R& J3 h4 \0 z C+ F7 h/ m4 p
}1 y8 e9 U2 e. R2 S# u
8 }3 T* B- L- @+ D( Q/ _ i5 A8 e, a
运行结果:2008 09 10 //结果为分割后的3个字符串0 o+ [) L3 s% n7 b9 e- c s) w: p
% k! n; I; \, q4 S! I4 J
实例:
! r2 V/ J+ @4 g; {TestString1.java:1 u- K4 o( k; j4 M' Y
程序代码
5 b3 j* m( B7 I6 W* P. epublic class TestString1& H9 g. j. Q6 {, l. S) L
{2 v2 H' N: b6 y6 X" k
public static void main(String args[]) {7 ?! b4 S7 d. I1 F" o6 o3 @
String s1 = "Hello World" ;0 ~- n! V6 Y) g0 E: _/ f
String s2 = "hello world" ;9 x& R2 h3 |" y3 _$ L
System.out.println(s1.charAt(1)) ;
% H& V0 p' I, E8 s4 ? System.out.println(s2.length()) ;- ]$ @. Y M, @( U9 M
System.out.println(s1.indexOf("World")) ;
( e# N* L( |/ ^$ j$ G2 B" z: J System.out.println(s2.indexOf("World")) ;
i i2 a$ d4 k; N- T8 D$ z System.out.println(s1.equals(s2)) ;+ R7 r: i+ @ @4 ?! A5 [
System.out.println(s1.equalsIgnoreCase(s2)) ;, t% f2 m: h6 R
) O, P" V3 w: S( t1 x
String s = "我是J2EE程序员" ;* o ^. V* D: z$ K y' E" W( V) Y
String sr = s.replace('我','你') ;. p' M o; ?8 K$ _* e: [
System.out.println(sr) ;
0 r) E+ P% S: j* }1 K! l1 g" A }2 S) {7 {2 {% } }
}- s0 i* b- a [6 K
) y z# @& E! R& A# d; C
TestString2.java:& d2 ], C' ?+ E# q! E
程序代码
3 `+ j! Z& W+ `9 k3 q9 E
_& V6 I, W- T3 o9 [! _) Upublic class TestString2; J p' z( E) ?1 X
{$ T# T) q. [7 I8 @9 Z9 n4 {+ Y
public static void main(String args[]) {5 x" z& M) \# H7 V) ]
String s = "Welcome to Java World!" ;
' K7 x: c& d0 ?3 ?5 v) } String s2 = " magci " ;! Y& N, A( W- Q
System.out.println(s.startsWith("Welcome")) ;
- {$ @" M- n# h6 X! z System.out.println(s.endsWith("World")) ;
' e8 t3 Y# s% x" i String sL = s.toLowerCase() ;
/ ^3 P& n6 x2 z String sU = s.toUpperCase() ;, v, J: ~8 k# }
System.out.println(sL) ;; z, U4 u8 J, U2 ]) B
System.out.println(sU) ;
/ x! D8 L& ?. c4 d$ S String subS = s.substring(11) ;( A* \0 {$ y/ ?; P* T2 x& w
System.out.println(subS) ;
: D1 l7 r9 w$ v+ s! _ String s1NoSp = s2.trim() ;0 P; s+ l/ j) C" x8 R
System.out.println(s1NoSp) ;6 R O" d% F( W7 n/ F& Z; t. o x
}
6 s- z+ Z c8 L$ \7 ?/ }0 V5 ^, L
1 f" f6 B4 ?( S2 M4 C |
|