`
橄榄绿
  • 浏览: 273484 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Java中String中类的常用方法

 
阅读更多

JavaString中类的常用方法

1) public String(char [] value):将字符数组转成字符串;

char c[]={‘f,w,s,w,g,a};

String s=new String(c);

System.out.println(s);

   

      结果为:fwswga

2) public String(cahr [] value,开始位置,新字符串的长度):将指定位置长度的字符数组转成字符串;

      char c[]={f,w,s,w,g,a }

      String s=new String(c,2,2);

      System.out.println(c);

      结果为:sw

3) public String (byte [ ] value):将字节数组转成字符串;

      byte b[]={w,o,r,l,d,!};

      String s=new String(b);

      System.out.println(s);

      

      结果为:world!

4) public String (byte [] value, 开始位置,新字符串长度);将指定位置长度的字节数组转成字符串;

byte b[]={w,o,r,l,d,!};

String s=new String(b,2,3);

System.out.println(s);

 

结果为:rl

5)public char[] toCharArray();将字符数组转成字符串

   String s=Hello;

   Char c[]=s.toCharArray();

   for (int i=0;i<c.length;i++)

System.out.print(c[i]+\t);
}

   结果为:  H e l l o

6)public char charAt(int index字符下标):返回字符串中指定位置的字符

  String s=Hello;

  System.out.println(s.charAt(3));

  结果为:l

7)public byte() getBytes():将一个字符串转成字节数组,默认输出值为ASCII码值

   String s=Hello;

byte b[]=s.getBytes();

for (int i=0;i<b.length;i++)

{

  System.out.print((char)b[i]+\t);

}

结果为:H e l o

8) public int length():求字符串的长度

String s=dwfsdfwfsadf;

System.out.pritnln(s.length());

结果为:12

9public int indexOf(String str):从头查找指定的字符串的位置,返回值为整型,从0开始,如果没找到,则返回-1

     String s=sdfwefsdgwe;

     System.out.println(s.indexOf(e));

    

     结果为:4

10public int indexOf(String str,指定位置) 从指定位置开始查找指定的字符串

     String s=dfwfgasdfwf;

     System.out.println(s.indexOf(a,8));

     结果为:-1

11public String trim():清除字符串左右两端的空格

     String s=   sdfsdfs   ;

     System.out.println(s.trim);

     结果为:sdfsdfs

12) public String substring(int beginindex):从指定位置到结尾截取字符串

   String s=fjeiflsjfowjflsdfjo;

   System.out.println(s.substring(5));

  

   结果为:lsjfowjflsdfjo

13public String substring(int begin,int end);指定截取字符串的开始和结束位置,不包含结束字符

       String s=foweflsdfjowefjls;

       System.out.println(s.substring(4,9));

      

       结果为:flsdf

14public String [] split(String str);按指定的字符分割字符串

       String s=fwefsdfefgefaselieffs;

       String ss[]=s.split(e);

       for (int i=0;i<ss.length;i++)

       {

           System.out.print(ss[i]+\t);

       }

       System.out.println();

     

       结果为:fw fsdf   fg    fas  li  ffs

15) public  String  toUpperCase():将字符串全部转换成大写

      System.out.println(new String(hello).toUpperCase());

      

      结果为:HELLO

16public String toLowerCase();将字符全部转成小写

      System.out.println(new String(HELLO).toLowerCase());

      结果为:hello

17) public boolean startsWith(String str);判断字符串是否由指定的字符串开头

      String s=fwofsdfjwkfs;

      System.out.println(s.startsWith(fw));

      System.out.println(s.startsWith(wf));

      结果为:true

              false

18) public boolean endsWith(String str):判断字符串是否由指定的字符串结尾

      String s=fwefsdgjgrg;

      System.out.println(s.endsWith(fe));

      System.out.println(s.endsWith(rg));

      结果为:false

              True

19 public boolean equals(String str):判断两个字符串是否相等,区分大小写

     String s1=hello;

     String s2=new String(“hello”);

     String s3=Hello;

     System.out.println(s1==s2---à+(s1.equals(s2)));

     System.out.println(s1==s3---à+(s1.equals(s3)));

     结果为:true

             False

20public boolean equalsIgnoreCase(String str):不区分大小写判断俩个字符串是否相等

String s1=hello;

String s2=HEllo;

System.out.println(s1==s2--à+(s1.equalsIgnoreCase(s2)));

结果为:true

21) public String replaceAll(String str1String str2);将字符串中str1替换成str2

String s=geowfjsklgoasdf;

System.out.println(s.replaceAll(f,s));

结果为:geowsjsklgoasds

<!--EndFragment-->
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics