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 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章