String类的常用方法

上一篇 / 下一篇  2013-12-19 21:58:15 / 个人分类:java

本章目标51Testing软件测试网6}Rk9I7_q? G?6h

 掌握String类中的常用方法51Testing软件测试网6LN#QGd"q

 了解API文档的查找方法

]wOJh9s8CT9a0

 51Testing软件测试网F_4ZS OyUU

字符串替换:public String replaceAll(String regex,String replacement)

|r3z YCUd|6u0I0

    public class StringAPIDemo12{51Testing软件测试网b+w H ^Z6T;[;Kc/T

        public static void main(String args[]){51Testing软件测试网b'a#j1pE)m ycRZ0H!f

            String str1 = "hello" ; //声明字符串

+Xj0Yj"E%w0

            String newStr = str1.replaceAll("l","x") ; //现在将所有的l替换成x

X)q$F[~0

            System.out.println("hello现在显示为" + newStr) ;

fP;z N\Y0

       

e|6z1\D]4^2v0

        }    

drd7\)K\,?Z0

    } ;

8a [G l%S l6m4`,Z`0

取得一个字符串的长度:public int length()

tp$N&X3^Lt$Y'mg0

public class StringAPIDemo04{51Testing软件测试网g9o;?3T*a*W

        public static void main(String args[]){51Testing软件测试网1HG0qW AP'o3Gp

            String str1 = "hello luoriver" ; //定义String对象

!j:T} Z8Mv'K\W~+V0

            int n = str1.length() ;

Y,} X%nb$`0

            System.out.println(n) ;  //将部分的byte数组变为字符串

_g H \:T!u0

        }

*}UA8Bq0

    } ;51Testing软件测试网V9e-Ten

 

vQp7V2t4P\0

 51Testing软件测试网} D;d1c'X{)neP:nl

查找指定的字符串是否存在51Testing软件测试网 Wb!I"g:k

   从头开始查找:public int indexOf(String str)51Testing软件测试网8{tfq3F,y9V

   public class StringAPIDemo05{

S!MXR@L]6B m0

        public static void main(String args[]){51Testing软件测试网6hb*HV5~FH%k;R7X~

            String str1 = "abcdefgcgh" ; //声明字符串

6t7l5x[B[XB1Sj+q0

            System.out.println(str1.indexOf("c")) ;  //查找返回位置51Testing软件测试网1g'_ g;Mb:n

            System.out.println(str1.indexOf("c",3)) ;  //查找返回位置,从第4个位置开始查找

a(Y8]*Cy'o8X1I0M+T:uK0

        }51Testing软件测试网'i L*a6Hc vu%U

    } ;

3S ndw b#Iw)Jp5E0

从指定位置开始查找:public int indexOf(String str,int fromIndex)

Dc3c:w+b4|9B0

查找的时候方法的返回值是一个int类型的数据,此数据表示的是一个字符串的具体位置,如果没有查找到此字符串,则返回"-1"

Pa v5PP(dW C"z_0

 51Testing软件测试网9~v+Tu,ln8E

   public class StringAPIDemo05{51Testing软件测试网*nzzm{_xs!\

        public static void main(String args[]){51Testing软件测试网Ig~@H2GG Y

            String str1 = "abcdefgcgh" ; //声明字符串

zl!ebGK]"wph0

            System.out.println(str1.indexOf("c")) ;  //查找返回位置51Testing软件测试网Y5@n0ZS,r9b*TxnS

            System.out.println(str1.indexOf("c",3)) ;  //查找返回位置,从第4个位置开始查找51Testing软件测试网 G]:U6C3fE4Z

            System.out.println(str1.indexOf("x")) ;  //没有查找到返回-1           51Testing软件测试网s.avQN+Bn

        }

d/e!U-_|0

    } ;

C7jV&I5N@~GFJ0

输出结果:

)EY:Qz*St N)Slp|0

251Testing软件测试网$`s7zOp(?E j q

751Testing软件测试网mM'xOH5X"s

-151Testing软件测试网 O%rs4g_7Q k;Z

 51Testing软件测试网6IxDl~4r\

不区分大小写的比较:51Testing软件测试网z9Yn$BZ

equals():只比较大小写一样的字符51Testing软件测试网"M c$B{2w*Yg(X

public boolean equalsIgnoreCase(String anotherString)51Testing软件测试网#Q%n6]gt M*WH

    public class StringAPIDemo11{51Testing软件测试网2P2N"p#Wh6{'C6dP,O

        public static void main(String args[]){

?9[L3H9S"B0

            String str1 = "helloWORLD" ; //声明字符串51Testing软件测试网*^4@k.c#s0Du

            String str2 = "HELLOworld" ; //声明字符串51Testing软件测试网)UI)\3p8C \

          51Testing软件测试网^R$d7QIC"I a(d'l

                System.out.println("\"helloWORLD\" equalsIgnoreCase \"helloworld\" " + str1.equalsIgnoreCase(str2)) ;           51Testing软件测试网4B/d6]bj2N7Q/^v2\

                System.out.println("\"helloWORLD\" equals \"helloworld\" " + str1.equals(str2)) ;

Dw/J^}:HD0

        }    51Testing软件测试网;HQ@E3j3~(nD

    } ;

:F2[#ei9}X$U K"n0

输出结果:51Testing软件测试网!h+ey`Gd

Subtopic

|1} ~ i+} U0

 51Testing软件测试网-y#GV2~"PT@

判断是否以指定的字符串开关或结尾

;l&v8A1Q4v u5f0

判断是否以指定的字符串开头:public boolean startsWith(String prefix)51Testing软件测试网0^mz6\&mh9V

判断是否以指定的字符串结尾public boolean endsWith(String suffix)51Testing软件测试网\Vj U9{Y7u

  public class StringAPIDemo10{51Testing软件测试网~TKCF"c4E7_

        public static void main(String args[]){

FaQPL(e0

            String str1 = "**hello" ; //声明字符串51Testing软件测试网f@wN_6t&w

            String str2 = "hello**" ; //声明字符串

3XsX5v M l1a0

            if(str1.startsWith("**")){//判断是否以"**"开头51Testing软件测试网/URa#d;l9`$z

                System.out.println("(**HELLO)**开头") ;51Testing软件测试网;\Bp`3v.cigWtk

            }

BIc}K y K`g5l.]w0

            if(str2.endsWith("**")){//判断是否以"**"结尾51Testing软件测试网Gi5Hz;X3dd"U,ZK8\

                System.out.println("(**HELLO)**结尾") ;51Testing软件测试网Ys~(V6J

            }

t%c.t Z#K`me0

            }

7B CkN@5S(ZE0o0

    } ;51Testing软件测试网kHkZ S#Na(enq

输出结果:

#QjHRZy"keM0

(**HELLO)**开头

}Zt)G quh._*OaC,B0

(**HELLO)**结尾

!O/CzU~8p0

 51Testing软件测试网_V)d,[,@'x

拆分字符串:public String[] split(String regex,int limit)51Testing软件测试网Vnh"ANv1S{0_

    public class StringAPIDemo09{

$c5i Z0k],kg;p|cn0

        public static void main(String args[]){

9OX|"~3]z0

            String str1 = "hello,luoriver" ; //声明字符串51Testing软件测试网4wbue? {\

            System.out.println("\"hello world\"转换成大写:" + str1.toUpperCase()) ;

$DVQu4`Wl,r:wTq0

            System.out.println("\"HELLO WORLD\"转换成小写:" + str1.toLowerCase()) ;

Te;O{:o0

            }

JBB%D]j8P9n0

    } ;

kS~b7T9D0

输出结果:51Testing软件测试网.Y&OVP+v2X

"hello world"转换成大写:HELLO,LUORIVER

(q8})C,Z!T3~qD b-I0

"HELLO WORLD"转换成小写:hello,luoriver

?/R.?&Hiu2Z$aI(Ki0

 51Testing软件测试网#qwmU2\}g D

字符截取

TV)v?'cX.pe0

从指定位置开始一直截取到最后:public String substring(int beginIndex)

#SA#O| j qN&e4x0

截取指定范围的字符串:public String substring(int beginIndex,int endIndex)51Testing软件测试网!|6z @x.f%K\ @U

    public class StringAPIDemo07{51Testing软件测试网AG/zn^u

        public static void main(String args[]){

l1[6F#DgO)I(D0

            String str1 = "hello,luoriver" ; //声明字符串

/~ R"c'v9M{^H B0

            System.out.println(str1.substring(2)) ;  //查找返回位置

4E sv Z*u0

            System.out.println(str1.substring(3,7)) ;

%FAQ"wv0

            System.out.println(str1.substring(22)) ;

[mN#Fm]3B0

        }

)W(KQ~w0

    } ;

)W]1F;~ \&i9@C0

输出结果:

!r#]6AP,s1ho.DM0

llo,luoriver

} gF-qoo0

lo,l

W["a yK V v'J;V0

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind51Testing软件测试网T9l,hP W~;r

ex out of range: -851Testing软件测试网!mtM\3j.H$n

        at java.lang.String.substring(Unknown Source)

|o j&o+|$m9gV0

        at StringAPIDemo07.main(StringAPIDemo07.java:6)51Testing软件测试网:a(x N&nXl7FA

 

$m i L,zDj,u0

 51Testing软件测试网/A8Ubl?

去掉空格51Testing软件测试网R@#z^:WDG

    public class StringAPIDemo06{

\&|~{2Q l)V(["R$n0

        public static void main(String args[]){51Testing软件测试网+~J W8J7|xy$S

            String str1 = "    hello,luoriver   " ; //声明字符串51Testing软件测试网s#L2t jUrmy

            System.out.println(str1) ;  //查找返回左右位置

rf[8p]8m0

            System.out.println(str1.trim()) ;  //去掉空格

9bWZ*F,m s.Y s8w0

        }

} sieo9U0

    } ;51Testing软件测试网7vxlsKLS

输出结果:

m{)S(o+LN4pn0

    hello,luoriver

2Hsw5d a&p5S#^0

hello,luoriver51Testing软件测试网P;h5b5PHbKzQ

 

&VnNLc*V@t0

字符串与byte数组的转换

B$h$x"e$V J2z,j1MtZ0

字符串变为字节数组:public byte[] getBytes()51Testing软件测试网6m9jXy5f!o

    public class StringAPIDemo03{51Testing软件测试网@ eKEFCf"I#k

        public static void main(String args[]){51Testing软件测试网E-e!}@)T

            String str1 = "hello" ; //定义String对象51Testing软件测试网] [uFz

            byte b[] = str1.getBytes() ; //将字符串变为byte51Testing软件测试网2RY i^?[ V#x

            System.out.println(new String(b)) ;  //将全部的byte数组变为字符串 

3?;\2Bh2to%HQ0

            System.out.println(new String(b,1,3)) ;  //将部分的byte数组变为字符串

a~ a:|v!D0

        }51Testing软件测试网 aMNvz

    } ;

%x@Ba Z0

输出结果:51Testing软件测试网[fGd1I3x

hello

0}{4XU ~ ?0

ell51Testing软件测试网XX l2He$W8r}EK a

 

0f4Ulj-N0

将一个字节数组变为字符串51Testing软件测试网y"Q^B8O|'~6LON c

将全部字节数组变为Stringpublic String(byte[] bytes)

S\.^1Z rh6G6e:L'F0

将部分字节数组变为Stringpublic String(byte[] bytes,int offset,int length)

)o| W$I1Qt/k9OXAC0

 

/w L@{G3L.x0

从字符串中取出指定位置的字符:public char charAt(int index)51Testing软件测试网 {6}/@iaD-b

    public class StringAPIDemo02{

wN4p2W2?)p:_ G0

        public static void main(String args[]){

4_CV^.V5c2b Zo8i0

            String str1 = "hello" ; //定义String对象51Testing软件测试网 Z1Fc @3}#|+h;[V

            System.out.println(str1.charAt(3)) ;  //取出字符串中第四个字符     51Testing软件测试网%UC8E}M^

        }51Testing软件测试网1v$[|dch"xK

    }51Testing软件测试网D!D{Y OBx'W

输出结果:l

[tj!`0n!b0

 51Testing软件测试网Vcx5_g4qZ

将字符串变为字符数组:public char[] to CharArray()

6Y&Ki7g[4dIG,s0

    public class StringAPIDemo01{

,H\fXMa0

        public static void main(String args[]){51Testing软件测试网Bo$D'x5Xs

            String str1 = "hello" ; //字义字符串

(yO(v2uBi m0

            char c[] = str1.toCharArray() ; //将一个字符串变为一个数组

P(q[j)M;U0

           51Testing软件测试网8A A6\/Ra#H)v"_

            for(int i=0; i<c.length;i++){  //循环输出

s _*aP~u,vt+f{0

                System.out.print(c[i] + "") ;  

&gE f^T1BImB#x0

            }51Testing软件测试网qQP1c6NE

        }51Testing软件测试网4F@4HQ2Vq

    }51Testing软件测试网k#Vw {*[G_k;o

 51Testing软件测试网*A4v0D{!o,W"d8R$z0zc

 51Testing软件测试网%z5P2|R#_/FW%c&S4@i7A

将字符数组变为字符串51Testing软件测试网 K6E'[0F!l Z| Sk

public String(char[] value,int offset, int count)51Testing软件测试网7f Y*Vp1] y%]([

public String(char[] value)

%u*Z;?9B'DUKr#x0

    public class StringAPIDemo01{51Testing软件测试网t/G:Zt5{

        public static void main(String args[]){51Testing软件测试网 h!qN8]p

            String str1 = "hello" ; //字义字符串51Testing软件测试网 J e'DI"tP:@1^O

            char c[] = str1.toCharArray() ; //将一个字符串变为一个数组51Testing软件测试网 ]dPy+z] NZ$m%_

           

3` }uz!_ i0

            for(int i=0; i<c.length;i++){  //循环输出

d5p*mO,D0

                 System.out.print(c[i] + "") ;

Qa\.`5lmL7Q9]"~0

               

8nh B1O5bD0

                }51Testing软件测试网$};otVBqb)v

               

2gPS!N2D J P0

            System.out.println("") ;        //换行

gj3MdM+Tl0

            String str2 = new String(c) ;   //将全部的字符数组变为String

T gv"m b&n$P/z"Wx0

            String str3 = new String(c,0,3) ; //将部分字符数组变为为字符串

|!J h)O|0

           51Testing软件测试网QK)Y(LK$S

            System.out.println(str2) ;      //输出字符串               51Testing软件测试网7D*aA(T ]C}y@

            System.out.println(str3) ;     //输出字符串51Testing软件测试网'q,S{'[ul

           

i$sBnU0

        }

erA@+Fh0

    }51Testing软件测试网R%v _ Cu3_

输出结果:51Testing软件测试网h5Q7kB7LF H0e

    public class StringAPIDemo01{

.| }oh~cd)[.y0

        public static void main(String args[]){51Testing软件测试网Z9d t&D3h^ oe

            String str1 = "hello" ; //字义字符串

+uYtvJ1K0

            char c[] = str1.toCharArray() ; //将一个字符串变为一个数组

%n}ZPS,B;h X1H4g0

           

@P1uJ+r0

            for(int i=0; i<c.length;i++){  //循环输出51Testing软件测试网!vE:@P$v/c

                 System.out.print(c[i] + "") ;51Testing软件测试网qHo-xZ!I;l

               51Testing软件测试网w3GFN?\*Y1y F

                }

? ? G5IO4b^0

               51Testing软件测试网Q lQ yYSgb

            System.out.println("") ;        //换行51Testing软件测试网(h e6\ t,XT_

            String str2 = new String(c) ;   //将全部的字符数组变为String

Ia5VF [hy0

            String str3 = new String(c,0,3) ; //将部分字符数组变为为字符串51Testing软件测试网s+`,JwZ0W9e

           

oC j'z OP0

            System.out.println(str2) ;      //输出字符串               51Testing软件测试网*dx"B N STN'wUr

            System.out.println(str3) ;     //输出字符串

-g#Y~&D D#UF%Wz0

           

'QN:V0[!I(s0

        }51Testing软件测试网,MK$ODce;u

    }

{7yn%u#[ o+p3U0

TAG:

 

评分:0

我来说两句

luoriver

luoriver

北漂一族,80后,计算机专业,从事SIP相关软件测试3年,热爱生活,崇尚运动。 爱看WWE、公开课。爱钻“牛角尖”,这就是我:luorivr!!!!!

日历

« 2024-04-21  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 345696
  • 日志数: 96
  • 图片数: 1
  • 建立时间: 2012-12-27
  • 更新时间: 2014-05-03

RSS订阅

Open Toolbar