91名师指路-头部
91名师指路

java 截取某个字符之前或之后的字符串和截取最后一个指定字符后的字符串

由于某些原因,现在不支持支付宝支付,如需要购买源码请加博主微信进行购买,微信号:13248254750

一:截取某个字符之前或之后的字符串。

String aa="00e8774d55c66d737ab1ce6878dc064c,16e52ce8e22395f1e466b983f70f1e0a";
// 截取逗号前的字符串
String str=aa.substring(0, aa.indexOf(","));
System.out.println(str); // 00e8774d55c66d737ab1ce6878dc064c

// 截取逗号后的字符串
String bb=aa.substring(str.length()+1, aa.length());
System.out.println(bb); // 16e52ce8e22395f1e466b983f70f1e0a


二:截取最后一个指定字符后的字符串。

public static void main(String[] args) {
String path="/usr/local/docker/excel/2020061610001.xlsx";
String substr=path.substring(path.lastIndexOf("/")+1);
System.out.println(substr);
}

执行结果:

2020061610001.xlsx


三:使用commons-lang3包下面的StringUtils进行字符串的截取

3.1)引入pom
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>

3.2)代码

public static void main(String[] args) {
String str="91名师指路,让能力变成价值,91mszl.com";
String res1=StringUtils.substring(str, 3); // 从第3位截取到字符串末尾
System.out.println(res1); // 师指路,让能力变成价值,91mszl.com

String res2=StringUtils.substring(str, 3, 10); // 从第3位截取到第10位
System.out.println(res2); // 师指路,让能力

String res3=StringUtils.substringBefore(str, ","); // 截取第一个逗号之前的字符
System.out.println(res3); // 91名师指路

String res4=StringUtils.substringBeforeLast(str, ","); // 截取最后一个逗号之前的字符
System.out.println(res4); // 91名师指路,让能力变成价值

String res5=StringUtils.substringAfter(str, ","); // 截取第一个逗号之后的字符
System.out.println(res5); // 让能力变成价值,91mszl.com

String res6=StringUtils.substringAfterLast(str, ","); // 截取最后一个逗号之后的字符
System.out.println(res6); // 91mszl.com


String res7=StringUtils.substringBetween(str, ","); // 截取两个字符串之间隔的字符
System.out.println(res7); // 让能力变成价值

String res8=StringUtils.substringBetween("91名师指路91名师指路91名师指路", "名", "指"); // 截取第一个字符“名”与第一个字符“指”之间的字符串
System.out.println(res8); // 师

String[] res9=StringUtils.substringsBetween("91名师指路", "名", "指"); // 截取第一个字符“名”与第一个字符“指”之间的字符串,以数组形式返回
for (int i=0; i<res9.length; i++){
System.out.println(res9[i]); // 师
}
}

执行结果:

师指路,让能力变成价值,91mszl.com
师指路,让能力
91名师指路
91名师指路,让能力变成价值
让能力变成价值,91mszl.com
91mszl.com
让能力变成价值


四:字符串截取

public static void main(String[] args) {
String str="10/20/30/40/50/60/70/80";

// 第一个"/"的位置
int index1 = str.indexOf("/");
String res1 = str.substring(0, index1); // 第一个"/"之前的字符串
System.out.println(res1); // 10
String res11 = str.substring(index1 + 1); // 第一个"/"之后的字符串
System.out.println(res11); // 20/30/40/50/60/70/80

// 第二个"/"的位置
int index2 = str.indexOf("/", str.indexOf("/") + 1);
String res2 = str.substring(0, index2); // 第二个"/"之前的字符串
System.out.println(res2); // 10/20
String res22 = str.substring(index2 + 1); // 第二个"/"之后的字符串
System.out.println(res22); // 30/40/50/60/70/80

// 倒数第一个"/"的位置
int lastIndex1 = str.lastIndexOf("/");
String res3 = str.substring(lastIndex1+1);
System.out.println(res3); // 80

// 倒数第二个"/"的位置
int lastIndex2 = str.lastIndexOf("/", str.lastIndexOf("/") - 1);
String res4 = str.substring(lastIndex2+1);
System.out.println(res4); // 70/80

// 从后往前截取,截取第三个"/"的字符串
int index=str.lastIndexOf("/");
index=str.lastIndexOf("/",index-1);
index=str.lastIndexOf("/",index-1);
String location=str.substring(index+1);
String result=location.substring(0, location.indexOf("/"));
System.out.println(result);
}

执行结果:

10
20/30/40/50/60/70/80
10/20
30/40/50/60/70/80
80
70/80
60





2019-09-24 22:56:48     阅读(7385)

名师出品,必属精品    https://www.91mszl.com

联系博主    
用户登录遮罩层
x

账号登录

91名师指路-底部