數組, 數字, 字符串的處理
阿新 • • 發佈:2017-07-15
截取字符串 sub 字符串轉換 lastindex == 長度 repl 替換 contains
public class Text2 { public static void main(String[] args){ // 常量池 String s1 = "abc"; String s2 = "abc"; System.out.println(s1==s2); // true //s1和s2指向的是同一個對象,都指向同一個內存地址 String s3 = new String("abc"); //在內存空間重新開辟一塊區域 String s4 = new String("abc"); //在內存空間重新開辟一塊區域 System.out.println(s3==s4); //false //s3和s4指向不一樣,指向的內存地址不一樣 System.out.println(s3.equals(s4)); // true //比較的是兩個字符串s3和s4的內容,都為abc } }
包裝類:
Integer.parseInt();
byte---Byte
short---Short
int---Integer
long---Long
float---Float
double---Double
boolean---Boolean
char---Character
public class Text2 { public static void main(String[] args){ //包裝類,都有相對應的方法 System.out.println(Integer.MIN_VALUE); System.out.println(Integer.MAX_VALUE); System.out.println(Byte.MIN_VALUE); //128 System.out.println(Byte.MAX_VALUE); //127 System.out.println(Long.MIN_VALUE); System.out.println(Long.MAX_VALUE); System.out.println(Short.MIN_VALUE); //-32768 System.out.println(Short.MAX_VALUE); //32767 System.out.println(Float.MIN_VALUE); System.out.println(Float.MAX_VALUE); System.out.println(Double.MIN_VALUE); System.out.println(Double.MAX_VALUE); System.out.println(Integer.parseInt("56")); //56,在這裏需要註意對應的方法名 System.out.println(Float.parseFloat("56")); //56.0 System.out.println(Float.parseFloat("12.34")); //12.34 } }
字符串的處理:
字符串之間的equals
String str = "..............";
str.length();
str.trim();
str.charAt(int i);
str.contains(CharSequence s);
str.startsWith(String s);
str.endsWith(String s);
replace(char o, char n);
replace(CharSequence o, CharSequence n);
split(String s);
toUpperCase();
toLowerCase();
valueOf(any args);
str.indexOf(String s);
str.lastIndexOf(String s);
str.substring(int i);
str.substring(int a, int b);
public class Text2 { public static void main(String[] args){ String str = " a new world a new start "; System.out.println(str.length()); //輸出值為25,包括兩邊的空格,即返回整個字符串的長度 System.out.println(str.trim()); //輸出值為 “a new world a new start”,即是去掉字符串兩邊的空格 System.out.println(str.trim().length()); //輸出值為23,即是去掉字符串兩邊的空格後返回整個字符串的長度 System.out.println(str.charAt(3)); //輸出值為 n ,即是取出字符串中指定索引位置的字符 System.out.println(str.contains("new")); //輸出值為 true,即是判斷一個字符串中是否包含所指定的另一個字符串, //如果包含,就為true,不包含,即為false System.out.println(str.contains("abc")); //輸出值為 false System.out.println(str.startsWith("qq")); //輸出值為 false,即是判斷某個字符串是否以另一個字符串開頭, //這段字符串以空格開始,而不是以qq開始,所以結果為false System.out.println(str.endsWith("qq")); //輸出值為 false,即是判斷某個字符串是否以另一個字符串結尾, //這段字符串以空格結尾,而不是以qq結尾,所以結果為false System.out.println(str.replace(‘a‘, ‘b‘)); //輸出值為 " b new world b new stbrt ", //即是把字符串裏面的a 全部替換為 b System.out.println(str.replace("new", "old")); //輸出值為 “ a old world a old start ” //即是 替換字符串中指定的字符或字符串 String[] strs = str.split(" "); //註意和 String[] strs = str.split(" ", 3); 的區別 System.out.println(strs.length); //輸出值為 7;最後一個空格省略不計 for(String s : strs){ System.out.println(s); } System.out.println(str.toUpperCase());// 將字符串轉換成大寫,即" A NEW WORLD A NEW START " System.out.println(str.toLowerCase());// 將字符串轉換成小寫,即" a new world a new start" System.out.println(String.valueOf(6));// toString(); System.out.println(str.indexOf("new"));// 子字符串在字符串中第一次出現的位置,輸出值為 3 System.out.println(str.lastIndexOf("new"));// 子字符串在字符串中最後一次出現的位置,輸出值為 15 System.out.println(str.substring(5)); //截取字符串包含索引為5的字符,輸出值為 w world a new start
System.out.println(str.substring(5, 8)); //從第一個數字開始截取, 直到第二個數字, 但是不包含第二個數字的位置的字符,,即從第5個到第7個,輸出值為 w w } }
數組, 數字, 字符串的處理