java中String的21種用法,javastring21種

來源:互聯網
上載者:User

java中String的21種用法,javastring21種
(建構函式必須new出來)

 * public String (char[] vaue)                         將一個字元數組變成字串(建構函式)

 * public String (char[] vaue,int offset,int count)    將制定範圍內的字元數組變為字串(建構函式)

 * public String (byte[],bytes)                        將一個byte數組變為字串(建構函式)

 * public String (byte[],bytes,int offset,int length)  將制定範圍內的byte數組變為字串(建構函式)

 * public char[] toCharArray()                         將一個字串變為字元數組

 * public char charAt(int index)                       從一個字串中取出頂頂位置的字元

 * public byte[](getBytes)                             將一個字串變成byte數組

 * public int length()                                 取得字串長度

 * public int indexOf(String str)                      從頭開始尋找指定字串位置找不到返回-1

 * public int indexOf(String  str,int fromIndex)       從指定位置尋找指定字串位置

 * public String trim()                                清除左右兩端的空格

 * public String substring(int beginIndex)             從指定位置開始一直取到尾進行字串的提取

 * public String substring(int begin,int end)          指定截取字串的開始點和結束點

 * public String[] split(String regex)                 按照指定的字串對字串進行拆分

 * public String toUpperCase()                         將一個字串全部變為大寫字母

 * public String toLowerCase()                         將一個字串全部變為小寫

 * public boolean startsWith(String prefix)            判斷是否以字串開頭

 * public boolean endsWith(String suffix)              判斷是否以字串結尾

 * public boolean equals(String str)                   判斷兩個字串是否相等

 * public boolean equalsIgnorCase(String str)           不區分大小寫比較字串是否相等

 * public String replaceAll(String regex,String replacement)字串替換


  例:

public class StrDemos {public static void main(String args[]){    char[] s={'g','1','c','c'};    String str=null;    str=new String(s);    System.out.println(str);//將一個字元數組變成字串            char[]d=str.toCharArray();//將一個字串轉換為字元數組    for(int i=0;i<d.length;i++){    System.out.println(d[i]);    }    }}輸出:g1ccg1cc



java中的string用法

看看這個

public class T1 {

public static void main(String[] agrs) {
String s = "a,1,5,6,2,3,b,d,f,w,c,e,^,&,@,8,";
String [] array=s.split(",");
String intArray ="" ;
String strArray = "";
String other = "";

for(int i=0;i<array.length;i++){
if ('0' <= array[i].charAt(0) && array[i].charAt(0) <= '9')
intArray = intArray + array[i].charAt(0) + ",";
else if (('a' <= array[i].charAt(0) && array[i].charAt(0) <= 'z')
|| ('A' <= array[i].charAt(0) && array[i].charAt(0) <= 'Z'))
strArray = strArray + array[i].charAt(0) + ",";
else{
other = other + array[i].charAt(0)+",";
}
}
System.out.println(intArray);
System.out.println(strArray);
System.out.println(other);
}

}
 
Java中String的用法 要詳細的 最好舉幾個例子

public class test {
public static void main(String args[])
{
String a="1";
String b="2";
int a_1=Integer.parseInt(a);//轉換成int類型進行相加減
int b_1=Integer.parseInt(b);
int c=a_1+b_1;
System.out.println(a+"+"+b+"="+c);
}
}
或者
public class test {
public static void main(String args[])
{

int a=1;
int b=2;
int c=a+b;
System.out.println(a+"+"+b+"="+c);
}
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.