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);
}
}