如何从字符串中获取第二个单词?
发布时间:2020-12-15 04:52:35 所属栏目:Java 来源:网络整理
导读:拿这些例子 Smith JohnSmith-Crane JohnSmith-Crane John-HenrySmith-Crane John Henry 我想得到约翰 空格后面的第一个单词,但可能直到结束,它可能直到非字母字符.这将如何在Java 1.5中? 解决方法 您可以使用正则表达式和 Matcher 类: String s = "Smith-C
拿这些例子
Smith John Smith-Crane John Smith-Crane John-Henry Smith-Crane John Henry 我想得到约翰 解决方法
您可以使用正则表达式和
Matcher 类:
String s = "Smith-Crane John-Henry"; Pattern pattern = Pattern.compile("s([A-Za-z]+)"); Matcher matcher = pattern.matcher(s); if (matcher.find()) { System.out.println(matcher.group(1)); } 结果: John (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |