Java的。将多个单词字符串拆分为每个空格的两个单词字符串 [英] Java. Splitting a multiple word string into two word strings every space

查看:291
本文介绍了Java的。将多个单词字符串拆分为每个空格的两个单词字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一轮回答这里昨晚。 @Reimeus提供的解决方案很棒,并提供以下输出:

The first round of this was answered here last night. The solution that @Reimeus gave was great and gives the below output from:

My name is the mighty llama

收件人:

String[] stringArray = string.split("(?<!\\G\\S+)\\s");

My name 
is the
mighty llama

但是,我现在发现自己需要纠正我正在做的事情。我实际上需要能够像这样分割字符串。

However, I now find myself need to correct what I'm doing. I actually need to be able to split the string like so.

My name
name is
is the
the mighty
mighty llama


推荐答案

你不能用 String.split()复制一个令牌,因为正则表达式用于搜索。

you cannot duplicate a token with String.split(), because regex is used to SEARCH.

你没有某种编辑就无法做到这一点,也许 String.replaceAll()将会完成。

you cannot do this without some kind of edit, maybe a String.replaceAll() will accomplish.

这篇关于Java的。将多个单词字符串拆分为每个空格的两个单词字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆