如何利用正则表达式,按照内容截取子字符串

听摇滚的卡车司机 2019-04-17 10:35:34
比如原字符串内容为“Tom is 25 years old,he likes football.”现在只想截取“he likes football.”这部分内容进行输出
...全文
279 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 6 楼 第一名zz的回复:
方法很多,这些都是String类里面的一些方法,可以自己看看API

String str = "Tom is 25 years old,he likes football.Tom is 25 years old,he likes reading books";
	String replaceAll = str.replaceAll("Tom is 25 years old,", "");//把不需要的字符串替换掉
	System.out.println(replaceAll);
感谢您的回答,学习了
qq_39936465 2019-04-18
  • 打赏
  • 举报
回复
引用 楼主 听摇滚的卡车司机 的回复:
比如原字符串内容为“Tom is 25 years old,he likes football.”现在只想截取“he likes football.”这部分内容进行输出

public class test9 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String s1="Tom is 25 years old,he likes football.";
		String s2="he likes football.";
		int begin=s1.indexOf(s2);
		int end=begin+s2.length();
		System.out.println(s1.substring(begin, end));
	}
}

阿部仲麻吕 2019-04-18
  • 打赏
  • 举报
回复
第一名zz 2019-04-18
  • 打赏
  • 举报
回复
方法很多,这些都是String类里面的一些方法,可以自己看看API

String str = "Tom is 25 years old,he likes football.Tom is 25 years old,he likes reading books";
	String replaceAll = str.replaceAll("Tom is 25 years old,", "");//把不需要的字符串替换掉
	System.out.println(replaceAll);
  • 打赏
  • 举报
回复
引用 3 楼 第一名zz的回复:
String str=“Tom is 25 years old,he likes football.”; String[] split = str.split(",")//以逗号分隔 String s = split[1];//你要得到的字符串
原字符串变得更加复杂时,比如“Tom is 25 years old,he likes football.Tom is 25 years old,he likes reading books.”将Tom25岁的信息全部剔除,只保留“他喜欢足球”和“他喜欢读书”两个短句,筛选条件该怎样定呢
Timor__ 2019-04-18
  • 打赏
  • 举报
回复
先获取开始截取字母的索引,再获取要截取字符串的长度,然后这个函数stringObject.substr(start,length)截取
第一名zz 2019-04-18
  • 打赏
  • 举报
回复
String str=“Tom is 25 years old,he likes football.”; String[] split = str.split(",")//以逗号分隔 String s = split[1];//你要得到的字符串

62,612

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧