您好,登錄后才能下訂單哦!
public class StringTest{ public static void main(String[] args){ //1 String s1 = "abc"; //2 String s2 = new String("cdg"); //3 byte[] bytes={98,88,34,25}; String s3 = new String(bytes); //4String(byte[] bytes, int offset, int length)Constructs a new String by decoding the specified subarray of bytes using the platform's default charse; String s4 = new String(bytes,1,3); //5. char[] c1 = {'我','是','中','國','人'}; String s5 = new String(c1); System.out.println(s5); //我是中國人 //6. String s6 = new String(c1,2,2); System.out.println(s6); //中國System.out.println(s3); //abcd String已經重寫了Object中的toString } } /*字符串常用方法 */ public class StringTest06{ public static void main(String[] args){ //1.char charAt(int index); String s1 = "我是王勇,是壞人!"; char c1 = s1.charAt(2); System.out.println(c1); //王 //2.boolean endsWith(String endStr); System.out.println("HelloWorld.java".endsWith("java")); //true System.out.println("HelloWorld.java".endsWith(".java")); //true System.out.println("HelloWorld.java".endsWith("HelloWorld.java")); //true System.out.println("HelloWorld.java".endsWith("txt")); //false System.out.println("HelloWorld.java".endsWith("java ")); //false //3. boolean equalsIgnoreCase(String anotherString); System.out.println("abc".equalsIgnoreCase("ABc")); //true //4.byte[] getBytes(); byte[] bytes = "abc".getBytes(); for(int i=0;i<bytes.length;i++){ System.out.println(bytes[i]); } //5.int indexOf(String str); System.out.println("http://192.168.1.100:8080/oa/login.action?username=jack&pwd=123".indexOf("/oa"));//25 //6.int indexOf(String str, int fromIndex); System.out.println("javaoraclec++javavb".indexOf("java",1)); //13 //7.int lastIndexOf(String str) System.out.println("javaoraclec++javavb".lastIndexOf("java")); //13 //8.int lastIndexOf(String str, int fromIndex) System.out.println("javaoraclec++javavb".lastIndexOf("java",14)); //13 //9.int length(); System.out.println("abc".length()); //數組是length屬性,String是length()方法 //10. String replaceAll(String s1,String s2); //mysqloraclec++mysqlvb System.out.println("javaoraclec++javavb".replaceAll("java","mysql")); //這個程序是4個字符串 //11.String[] split(String s); String myTime = "2008,08,08"; String[] ymd = myTime.split(","); for(int i=0;i<ymd.length;i++){ System.out.println(ymd[i]); } //12.boolean startsWith(String s); System.out.println("/system/login.action".startsWith("/")); //true //13.String substring(int begin); System.out.println("/oa/login.action".substring(3)); // /login.action //14. String substring(int beginIndex, int endIndex) System.out.println("/oa/login.action".substring(4,9)); //login //15.char[] toCharArray();//字符串到字符數組的轉化 char[] c2 = "我是李海波".toCharArray(); for(int i=0;i<c2.length;i++){ System.out.println(c2[i]); } //16.轉換成大寫 System.out.println("Abcdef".toUpperCase()); //17.轉換成小寫 System.out.println("ABCDEf".toLowerCase()); //18.String trim();、、去掉兩邊的空格 System.out.print(" a bcd e ".trim()); System.out.println("TEST"); //19.String valueOf(Object obj); Returns the string representation of the Object argument. Object o = null; System.out.println(o); //不會,因為并不是直接調用toString方法,String.valueOf(Object)這個方法對空值進行處理了。 System.out.println(String.valueOf(o)); //System.out.println(o.toString()); //會出現空指針
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。