参考链接:
【↑↑↑ 上面这个很值得看 ↑↑↑】
......
注意:Java中字符串第一个字符是从下标0开始算起的。
1. 字符串裁剪
1.1 字符串自带的substring()
String str = "2023年2月16日21:10:05 Jiangnan Cui";
// 1. str.substring(beginIndex): 从字符串str的第beginIndex个字符开始向后裁剪至末尾,包括第beginIndex个字符
String substring = str.substring(19);
System.out.println("substring = " + substring);// substring = Jiangnan Cui
// 2. str.substring(beginIndex, endIndex):截取字符串str指定索引范围[beginIndex, endIndex)内字符串(含头不含尾)
String substring1 = str.substring(10, 18);
System.out.println("substring1 = " + substring1);// substring1 = 21:10:05
1.2 字符串自带的indexOf() + substring()
String str = "2023年2月16日21:10:05 Jiangnan Cui";
/**
* indexOf() 方法有以下四种形式:
* 1.public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
* 2.public int indexOf(int ch, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
* 3.int indexOf(String str): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
* 4.int indexOf(String str, int fromIndex): 返回从 fromIndex 位置开始查找指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
*/
// --- str.indexOf('字符'):返回字符串str中第一次出现此字符的下标索引位置
int i = str.indexOf("0");
System.out.println("i = " + i);// i = 1
// --- str.indexOf('字符', fromIndex):返回字符串str中第fromIndex字符后第一次出现此字符的下标索引位置
int i1 = str.indexOf('0', 5);
System.out.println("i1 = " + i1);// i1 = 14
// 未出现/找不到返回-1
int i2 = str.indexOf('z', 5);
System.out.println("i2 = " + i2);// i2 = -1
// --- str.indexOf("字符串", fromIndex):返回字符串str中第fromIndex字符后第一次出现此字符串的下标索引位置
int i3 = str.indexOf("nan", 5);
System.out.println("i3 = " + i3);// i3 = 13
// 未出现/找不到返回-1
int i4 = str.indexOf("cjn", 5);
System.out.println("i4 = " + i4);// i4 = -1
/**
* lastIndexOf() 方法有以下四种形式:
* 1.public int lastIndexOf(int ch): 返回指定字符在此字符串中最后一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
* 2.public int lastIndexOf(int ch, int fromIndex): 返回指定字符在此字符串中最后一次出现处的索引,从指定的索引处开始进行反向搜索,如果此字符串中没有这样的字符,则返回 -1。
* 3.public int lastIndexOf(String str): 返回指定子字符串在此字符串中最右边出现处的索引,如果此字符串中没有这样的字符,则返回 -1。
* 4.public int lastIndexOf(String str, int fromIndex): 返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始反向搜索,如果此字符串中没有这样的字符,则返回 -1。
*/
// --- str.lastIndexOf('字符'):返回字符串str中最后一次出现该字符的索引下标
int j = str.lastIndexOf('n');
System.out.println("j = " + j);// j = 26
// 未出现/找不到返回-1
int j1 = str.lastIndexOf('K');
System.out.println("j1 = " + j1);// j1 = -1
// --- str.lastIndexOf("字符串"):返回字符串str中最后一次出现该字符串的索引下标
int j2 = str.lastIndexOf("nan");
System.out.println("j2 = " + j2);// j = 24
// 未出现/找不到返回-1
int j3 = str.lastIndexOf("cjn");
System.out.println("j3 = " + j3);// j3 = -1
// --- str.lastIndexOf('字符',fromIndex):返回字符串str中第fromIndex字符前最后一次出现此字符的下标索引位置
int j4 = str.lastIndexOf('n', 27);
System.out.println("j4 = " + j4);// j4 = 26
// 未出现/找不到返回-1
int j5 = str.lastIndexOf('K', 27);
System.out.println("j5 = " + j5);// j5 = -1
// --- str.lastIndexOf("字符串",fromIndex):返回字符串str中第fromIndex字符前最后一次出现此字符串的下标索引位置
int j6 = str.lastIndexOf("an", 27);
System.out.println("j6 = " + j6);// j6 = 25
// 未出现/找不到返回-1
int j7 = str.lastIndexOf("na", 27);
System.out.println("j7 = " + j7);// j7 = 24
/**
* 综合substring、indexOf、lastIndexOf来获取指定的字符串
*/
// 1、获取第一个":"之后所有的字符
int i5 = str.indexOf(':');
String substring2 = str.substring(i5);
System.out.println("substring2 = " + substring2);// :10:05 Jiangnan Cui
// 2、获取最后一个":"之后所有的字符
int i6 = str.lastIndexOf(':');
String substring3 = str.substring(i6);
System.out.println("substring3 = " + substring3);// :05 Jiangnan Cui
// 3、获取第2个"n"之后所有的内容
String substring4 = str.substring(str.indexOf('n', str.indexOf('n')));
System.out.println("substring4 = " + substring4);// nan Cui
// 4、获取第2个"n"前面所有的内容
String substring5 = str.substring(0, str.indexOf('n', str.indexOf('n')));
System.out.println("substring5 = " + substring5);// 2023年2月16日21:10:05 Jiang (含有不含尾)
1.3 字符串自带的split()
String str = "2023年2月16日21:10:05 Jiangnan Cui";
/**
* 字符串自带的split方法将字符串拆分成字符串数组
* .split(regex)方法: regex可为正则表达式
* .split(regex, limit)方法: limit用来限制返回数组中元素的个数
*/
// 1、只拆分数组
String[] s2 = str.split(" ");
String s3 = Arrays.toString(s2);
System.out.println("s3 = " + s3);// [2023年2月16日21:10:05, Jiangnan, Cui]
// 2、既拆分数组,又限制个数
String[] s4 = str.split(" ", 2);
String s5 = Arrays.toString(s4);
System.out.println("s5 = " + s5);// [2023年2月16日21:10:05, Jiangnan Cui]
1.4 利用StringUtils字符串工具类的substring---截取
String str = "2023年2月16日21:10:05 Jiangnan Cui";
/**
* 利用StringUtils工具类截取
*/
// 1、截取字符串str第一个"内容"前的所有内容
String s = StringUtils.substringBefore(str, " ");
System.out.println("s = " + s);// 2023年2月16日21:10:05
// 2、截取字符串str第一个"内容"后的所有内容
String s1 = StringUtils.substringAfter(str, " ");
System.out.println("s1 = " + s1);// Jiangnan Cui
// 其它详见:
// StringUtils.substring()
// StringUtils.substringBefore()
// StringUtils.substringAfter()
// StringUtils.substringAfterLast()
// StringUtils.substringBeforeLast()
// StringUtils.substringBetween()
未完待续......
如有问题,欢迎批评指正!