黑马程序员------毕老师视频笔记第13-17天------JavaAPI(String类、StringBuffer类)(2)

来源:互联网 发布:网络推广软件破解版 编辑:程序博客网 时间:2024/06/06 05:38

---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------

String示例

class StringDemo{public static void main (String [] args){String str1 = "abcdefg";sop("\n***************查询示例**************************\n");sop("原字符串str1:"+str1+"\n");sop("str1.length():"+str1.length()+"\n");sop("str1.charAt(2):"+str1.charAt(2)+"\n");//sop(str1.charAt(7));//抛出StringIndexOutOfBoundsException异常sop("str1.indexOf('c'):"+str1.indexOf('c')+"\n");sop("str1.lastIndexOf('c'):"+str1.lastIndexOf('c')+"\n");sop("str1.indexOf(\"bc\"):"+str1.indexOf("bc")+"\n");sop("str1.lastIndexOf(\"bc\"):"+str1.lastIndexOf("bc")+"\n");sop("str1.indexOf('k'):"+str1.indexOf('k')+"\n");sop("str1.substring(2,5):"+str1.substring(2,5)+"\n");//含头不含尾String str2 = "abcd";String str3 = "Abcd";sop("\n***************判断示例**************************\n");sop("原字符串str2:"+str2+"\n");sop("原字符串str3:"+str3+"\n");sop("str2.equals(str3):"+str2.equals(str3)+"\n");sop("str2.equalsIgnoreCase(str3):"+str2.equalsIgnoreCase(str3)+"\n");sop("str2.contains(\"bc\"):"+str2.contains("bc")+"\n");sop("str2.startsWith(\"ab\"):"+str2.startsWith("ab")+"\n");sop("str2.endsWith(\"ab\"):"+str2.endsWith("ab")+"\n");sop("str2.isEmpty():"+str2.isEmpty()+"\n");String str4 = "abc-tyu-jk";String str5 = "  asdf  ";char[] ch = {'a','f','g','t','r'};sop("\n***************转换示例**************************\n");sop("原字符串str4:"+str4+"\n");sop("原字符串str5:"+str5+"\n");sop("原字符数组:ch");sop(ch);sop();sop("str4.split(\"-\"):");sop(str4.split("-"));sop();sop("str4.toCharArray():");sop(str4.toCharArray());sop();sop("str2.getBytes():");sop(str2.getBytes());sop();sop("new String(ch):"+new String(ch)+"\n");sop("new String(ch,1,4):"+new String(ch,1,4)+"\n");sop("String.valueOf(ch):"+String.valueOf(ch)+"\n");sop("String.valueOf(ch,1,4):"+String.valueOf(ch,1,4)+"\n");sop("str3.toUpperCase():"+str3.toUpperCase()+"\n");sop("str3.toLowerCase():"+str3.toLowerCase()+"\n");sop("str4.replace('-','*'):"+str4.replace('-','*')+"\n");sop("str4.replace(\"abc\",\"lll\"):"+str4.replace('-','*')+"\n");sop("str5.trim():"+str5.trim()+"\n");sop("str4.concat(str5):"+str4.concat(str5)+"\n");sop("\n***************比较示例**************************\n");sop("str4.compareTo(str5)"+str4.compareTo(str5)+"\n");}public static void sop(){System.out.println();}public static void sop(String str){System.out.print(str);}public static void sop(String[] str){for(int i=0; i<str.length; i++){System.out.print(str[i]+" ");}}public static void sop(char[] ch){for(int i=0; i<ch.length; i++){System.out.print(ch[i]+" ");}}public static void sop(byte[] b){for(int i=0; i<b.length; i++){System.out.print(b[i]+" ");}}}


StringBuffer示例:

class StringBufferDemo{public static void main(String[] str){StringBuffer sb = new StringBuffer("123");sop("\n***************添加示例**************************\n");sop("原StringBuffer sb = new StringBuffer(\"123\"):"+sb+"\n");sb.append("asdf");sop("sb.append(\"asdf\"):"+sb+"\n");sb.append("-zcxvb");sop("sb.append(\"-zcxvb\"):"+sb+"\n");sb.insert(5,"@@@@@");sop("sb.insert(5,\"@@@@@\"):"+sb+"\n");sop("\n***************删除示例**************************\n");sb.delete(3,7);sop("sb.delete(3,7):"+sb+"\n");sb.deleteCharAt(4);sop("sb.deleteCharAt(4):"+sb+"\n");sop("\n***************查找示例**************************\n");sop("sb.charAt(3):"+sb.charAt(3)+"\n");sop("sb.indexOf(\"xv\"):"+sb.indexOf("xv")+"\n");sop("sb.lastIndexOf(\"xv\"):"+sb.lastIndexOf("xv")+"\n");sop("\n***************修改示例**************************\n");sb.replace(5,8,"00");sop("sb.replace(5,8,\"00\"):"+sb+"\n");sb.setCharAt(2,'A');sop("sb.setCharAt(2,'A'):"+sb+"\n");sop("\n***************翻转示例**************************\n");sb.reverse();sop("sb.reverse:"+sb+"\n");sop("\n***************转化示例**************************\n");char[] ch = new char[5];sop("ch:");sop(ch);sop();sb.getChars(3,6,ch,0);sop("sop(sb.getChars(3,6,ch,0))之后ch:");sop(ch);sop();}public static void sop(){System.out.println();}public static void sop(String str){System.out.print(str);}public static void sop(char[] ch){for(int i=0; i<ch.length; i++){System.out.print(ch[i]+" ");}}}


---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
0 0
原创粉丝点击