String-

  • 字符串與字符數組的轉換 
    toCharArray()<—>String(char[] c)..
public class StringAPIDemo01{
public static void main(String args[]){
String str1="hello";
char c[]=str1.toCharArray();//字符串變字符數組
for(int i=0;i<c.length;i++){
System.out.print(c[i]+"\t");
}

System.out.println();
String str2=new String(c);//字符數組變字符串
String str3=new String(c,0,3);
System.out.println(str2);
System.out.println(str3);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

run

  • 從字符串中取出指定位置的字符 
    charAt()
public class StringAPIDemo02{
public static void main(String args[]){
String str1="hello";
System.out.println(str1.charAt(1));//取出第2個字符
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

run

  • 字符串與byte數組轉換 
    getBytes()<—>String(byte[] b)..
public class StringAPIDemo03{
public static void main(String args[]){
String str1="hello";
byte b[]=str1.getBytes();//String-->byte[]
for(int i=0;i<b.length;i++){
System.out.print(b[i]+"\t");
}
System.out.println(new String(b));//byte[]-->String
System.out.println(new String(b,1,3));
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

run

  • 取得字符串長度 
    length()
public class StringAPIDemo04{
public static void main(String args[]){
String str1="hello Lixinghua";
System.out.println("\""+str1+"\"的長度爲:"+str1.length());
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

run

  • 查找指定字符串是否存在 
    indexOf()
public class StringAPIDemo05{
public static void main(String args[]){
String str1="abcdefgcgh";
System.out.println(str1.indexOf("c"));//返回查找位置
System.out.println(str1.indexOf("c",3));//從第4位開始找
System.out.println(str1.indexOf("x"));//未找到返回-1
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

run

  • 去掉左右空格 
    trim()
public class StringAPIDemo06{
public static void main(String args[]){
String str1="   hell o    ";
System.out.println(str1.trim());//去掉左右空格
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

run

  • 字符串截取 
    substring()..
public class StringAPIDemo07{
public static void main(String args[]){
String str1="hello world";
System.out.println(str1.substring(6));//從第7位開始截取
System.out.println(str1.substring(0,5));//截取0-5
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

run

  • 按照指定的字符串拆分字符串 
    split()
public class StringAPIDemo08{
public static void main(String args[]){
String str1="hello world";
String s[]=str1.split(" "|"\t");//按空格或製表符拆分
for(String str:s){
System.out.println(str);
}
}
} 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

run

  • 字符串的大小寫轉換 
    toUpperCase()<—>toLowerCase()
public class StringAPIDemo09{
public static void main(String args[]){
System.out.println("將\"hello world\"轉成大寫:"+"hello world".toUpperCase());
System.out.println("將\"HELLO WORLD\"轉成小寫:"+"HELLO WORLD".toLowerCase());
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

run

  • 判斷是否以指定的字符串開頭或結尾 
    startsWith()<—>endsWith()
public class StringAPIDemo10{
public static void main(String args[]){
String str1="**HELLO";
String str2="Hello**";
if(str1.startsWith("**")){//判斷是否以**開頭
System.out.println("(**HELLO)以**開頭");
}
if(str2.endsWith("**")){//判斷是否以**結尾
System.out.println("(Hello**)以**結尾");
}
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

run

  • 不區分大小寫進行字符串比較 
    equalsIgnoreCase()
public class StringAPIDemo11{
public static void main(String args[]){
String str1="HELLO";
String str2="hello";
System.out.println("\"HELLO\" equals \"hello\""+str1.equals(str2));//區分大小寫比較
System.out.println("\"HELLO\" equalsIgnoreCase \"hello\""+str1.equalsIgnoreCase(str2));//不區分大小寫比較
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

run

  • 將指定字符串替換成其他字符串(注意:字符串的內容一旦聲明則不可改變) 
    可使用replaceAll(“xx”,”[這裏無空格]”)清除某字符。

    replaceAll()

public class StringAPIDemo12{
public static void main(String args[]){
String str="hello";
//str.replaceAll("l","x");
//System.out.println(str);//**仍然輸出“hello”**
String newStr=str.replaceAll("l","x");//將所有l換成x
System.out.println("替換之後的結果爲:"+newStr);
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

run

  • 從頭開始查找指定的字符串位置,返回值不爲-1表示找到了查詢內容。 
    int indexOf(String str)

  • 格式化字符串。 
    String.format(format,item1,item2,…)

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