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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。