java中String 字符串的截取操作
#1:通过split()。将正则传入split()。返回的是一个字符串数组类型。不过通过这种方式截取会有很大的性能损耗,因为分析正则非常耗时。[**当分割符是 . 或者是 | 时,必须使用 \\\ 进行转义**]
1 String str="How .are you. doing today?"; 2 String[] arr = str.split("."); 3 for(String string : arr) { 4 System.out.println("返回的数组是"+string); 5 }
#2:方法二:通过subString()方法来进行字符串截取
1 String str = "loveyouand"; 2 int n = 4; 3 System.out.println(str.substring(0, str.length() - n)); 4 //result: loveyo
#3、查看字符串是不是以某一个字符串结尾的 endWith
1 String str1 = "abcasdfasdfabc"; 2 String str2 = "abcdsdfasdfasdfasdfasdabc"; 3 if(str1.startsWith(str2.substring(0,2))&&str1.endsWith(str2.substring(str2.length()-3)))
#4: import org.apache.commons.lang3.StringUtils.split( String str, String separatorChars);根据指定分隔符对字符串进行分割
1 String jobIds = "as_sd_ef_we"; 2 3 String[] split = StringUtils.split(jobIds, "_"); 4 5 for (String s : split) { 6 System.out.println("asas " + s); 7 } 8 9 //asas as 10 //asas sd 11 //asas ef 12 //asas we
#5:org.springframework.util.StringUtils.split(jobIds, "_") 该方法只能对字符串分割一次。
1 String jobIds = "as_sd_ef_we"; 2 3 String[] split = org.springframework.util.StringUtils.split(jobIds, "_"); 4 5 for (String s : split) { 6 System.out.println("asas " + s); 7 }
posted on 2020-11-10 10:57 夜空中闪闪发光的星星 阅读(5146) 评论(0) 收藏 举报
浙公网安备 33010602011771号