1 package com.jdk7.chapter5; 2 3 public class UseString { 4 5 /** 6 * 如果源字符串中匹配有个多个目标字符串,则取最后一个的起始索引号 7 * @param src 8 * @param dest 9 */ 10 public static void findStr(String src, String dest){ 11 if(dest==null){ 12 System.out.println("目标字符串不能为空!"); 13 } 14 System.out.println("src.indexOf(dest) = "+src.indexOf(dest)); 15 System.out.println("src.indexOf(dest, 4) = "+src.indexOf(dest, 4)); 16 System.out.println("src.lastIndexOf(dest) = "+src.lastIndexOf(dest)); 17 System.out.println("src.lastIndexOf(dest, 4) = "+src.lastIndexOf(dest, 4)); 18 } 19 20 public static void subStr(String src){ 21 if(src==null){ 22 System.out.println("源字符串不能为空!"); 23 } 24 System.out.println("src.substring(2) = "+src.substring(2)); 25 System.out.println("src.substring(4, 10) = "+src.substring(4, 10)); 26 } 27 28 public static void replaceStr(String src){ 29 if(src==null){ 30 System.out.println("源字符串不能为空!"); 31 } 32 System.out.println(src.replace('a', 'i')); 33 System.out.println(src.replaceFirst("s", "p")); 34 System.out.println(src.replaceAll("s", "y")); 35 } 36 37 public static void toUpperOrLower(String src){ 38 if(src==null){ 39 System.out.println("源字符串不能为空!"); 40 } 41 System.out.println(src.toUpperCase()); 42 System.out.println(src.toLowerCase()); 43 } 44 45 public static void charAt(String src){ 46 if(src==null){ 47 System.out.println("源字符串不能为空!"); 48 } 49 System.out.println("src.charAt(5) = "+src.charAt(5)); 50 //字符串和字符不能自动进行类型转换,所以没法正常打印 51 System.out.println(src.toCharArray()); 52 } 53 54 /** 55 * 比较规则为:从第一个字符编码开始比较,如果第一个字符编码比目标字符编码小则返回负数,如果相等则返回0,如果比目标字符编码大则返回正数, 56 * 返回的值为字符编码与目标字符编码之差 57 * @param src 58 * @param dest 59 */ 60 public static void compare(String src,String dest){ 61 if(src==null || dest==null){ 62 System.out.println("源字符串或目标字符串不能为空!"); 63 } 64 System.out.println("两个字符串进行比较的结果为: "+src.compareTo(dest)); 65 } 66 67 public static void equals(String src, String dest){ 68 if(src==null){ 69 System.out.println("源字符串不能为空!"); 70 } 71 // System.out.println("src.startsWith("T") = "+src.startsWith("T")); 72 // System.out.println("src.endsWith("!") = "+src.endsWith("!")); 73 System.out.println("src.equals(dest) = "+src.equals(dest)); 74 System.out.println("src.equalsIgnoreCase(dest) = "+src.equalsIgnoreCase(dest)); 75 } 76 77 public static void startWith(){ 78 79 } 80 81 public static void endWith(){ 82 83 } 84 85 public static void regex(){ 86 String src = "aab aaa bb ab"; 87 String pattern = "^[a-zA-Z| ]$"; 88 System.out.println("src = "+src+" pattern: "+pattern); 89 System.out.println("src匹配pattern = "+src.matches(pattern)); 90 System.out.println("用一个空格替换第一个连续两位以上的制表符 >"+src.replaceFirst("\s{2,}", " ")); 91 System.out.println("用一个空格替换所有连续两位以上的制表符 >"+src.replaceAll("\s{2,}", " ")); 92 93 String[] strA = src.split("\s{2,}"); 94 System.out.print("以连续制表符分隔后的数组为>"); 95 for(int i=0;i<strA.length;i++){ 96 System.out.print(strA[i]+" "); 97 } 98 System.out.println(); 99 100 String[] strB = src.split("\s{2,}", 3); 101 System.out.print("已连续制表符分隔最多3段的数组为>"); 102 for(int i=0;i<strB.length;i++){ 103 System.out.print(strB[i]+" "); 104 } 105 System.out.println(); 106 } 107 108 109 public static void main(String[] args) { 110 String src = "This is a string Object!"; 111 String dest = "is"; 112 System.out.println("==============查找String中目标字符串所在索引号=============="); 113 UseString.findStr(src, dest); 114 System.out.println("==============截取String中目标字符串=============="); 115 UseString.subStr(src); 116 System.out.println("==============替换String中的某些字符串=============="); 117 UseString.replaceStr(src); 118 System.out.println("==============将String字符串统一变大写或小写=============="); 119 UseString.toUpperOrLower(src); 120 System.out.println("==============获取String中某个位置的字符=============="); 121 UseString.charAt(src); 122 System.out.println("==============比较源字符串和目标字符串,比较原则从首字符开始比较字符编码,返回结果为字符编码差值=============="); 123 UseString.compare(src, dest); 124 UseString.compare(src, src); 125 UseString.compare(src, "A"); 126 System.out.println("==============比较两个字符串是否相等,分是否区分大小写两种情况=============="); 127 UseString.equals(src, "esgt"); 128 UseString.equals(src, "THIS IS A STRING OBJECT!"); 129 UseString.equals(src, src); 130 System.out.println("==============字符串和正则表达式是否匹配,以及使用正则表达是分隔字符串=============="); 131 UseString.regex(); 132 } 133 } 134 135 执行结果为: 136 ==============查找String中目标字符串所在索引号============== 137 src.indexOf(dest) = 2 138 src.indexOf(dest, 4) = 5 139 src.lastIndexOf(dest) = 5 140 src.lastIndexOf(dest, 4) = 2 141 ==============截取String中目标字符串============== 142 src.substring(2) = is is a string Object! 143 src.substring(4, 10) = is a 144 ==============替换String中的某些字符串============== 145 This is i string Object! 146 Thip is a string Object! 147 Thiy iy a ytring Object! 148 ==============将String字符串统一变大写或小写============== 149 THIS IS A STRING OBJECT! 150 this is a string object! 151 ==============获取String中某个位置的字符============== 152 src.charAt(5) = i 153 This is a string Object! 154 ==============比较源字符串和目标字符串,比较原则从首字符开始比较字符编码,返回结果为字符编码差值============== 155 两个字符串进行比较的结果为: -21 156 两个字符串进行比较的结果为: 0 157 两个字符串进行比较的结果为: 19 158 ==============比较两个字符串是否相等,分是否区分大小写两种情况============== 159 src.equals(dest) = false 160 src.equalsIgnoreCase(dest) = false 161 src.equals(dest) = false 162 src.equalsIgnoreCase(dest) = true 163 src.equals(dest) = true 164 src.equalsIgnoreCase(dest) = true 165 ==============字符串和正则表达式是否匹配,以及使用正则表达是分隔字符串============== 166 src = aab aaa bb ab pattern: ^[a-zA-Z| ]$ 167 src匹配pattern = false 168 用一个空格替换第一个连续两位以上的制表符 >aab aaa bb ab 169 用一个空格替换所有连续两位以上的制表符 >aab aaa bb ab 170 以连续制表符分隔后的数组为>aab aaa bb ab 171 已连续制表符分隔最多3段的数组为>aab aaa bb ab