java中String的一些方法深入解析_java

來源:互聯網
上載者:User

1、public String(char[] c,begin,length).
從字元數組c的下標begin處開始,將長度為length的字元數群組轉換為字串。
begin與length可以省略,即將字元數組c轉換為字串。另:字元數組可改為位元組數組byte[] b.
char[] c=new char[]{'j','y','6','a','4','t','9'};
String s1=new String(c);
String s=new String(c,2,3);
System.out.println(s1);
System.out.println(s);

2、public char[] toCharArray().
字串裝換成字元數組。



3、public char charAt(int 下標).
返回字串中指定位置的字元。
String s="jkdfsdf";
char t=s.charAt(3);

4、public byte[] getBytes().
將一個字串轉換成位元組數組,其預設輸出為ASCII值,可通過char強制類型轉換輸出位元組。String s="sjdfsdf";
byte[] b=s.getBytes();

5、public String trim().
清除字串左右兩端的空格。
String s="skkgnsdfsd   ";
System.out.println(s.trim());

6、public int indexOf(String s,int index).
從字串中尋找指定位置之後指定的字元所在的位置。若不指定位置,則從頭開始。
String s="dgdgdg";
int n=s.indexOf("t");//從頭開始尋找
int n1=s.indexOf("d",3);//從位置3處開始尋找

7、public String substring(int beginindex,int endindex ).
截取所指定的從開始位置到結束位置的字串,不包含結束字元。結束位置可以省略。
String s="sdgsgghd";
String s1=s.substring(2,4);
String s2=s.substring(2);

8、public String[] split(String s).
通過指定的字元分割字串。
String s="dfgdhdfgdrhrhgdt";
String ss[]=s.split("d");
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);

9、public String toUpperCase()./public String toLowerCase().字元大小寫轉換。
String s="dfgdhdfgdrhrhgdt";
String s1=s.toUpperCase();//字元全大寫
String s2=s.toLowerCase();//字元全小寫

10、public boolean startsWith(String s)./public boolean endsWith(String s).檢測字串是否是以指定的字元開始/結尾。
String s="dfdhffghrtgfjn mjg";
boolean t1=s.startsWith("e");
boolean t2=s.endsWith("h");

11、判斷字串是否相等,區分大小寫:equals()。不區分大小寫equalsIgnoreCase().
String s="dfgdghdf";
String s1="sfsgsdu";
s.equals(s1);

12、public String replaceAll(String s,String s1).將字串中的s都替換成s1.
String s="dfgdghdf";
String s1=s.replaceAll("d","f");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.