简体   繁体   English

如何在某个单词后使用.split拆分字符串?

[英]How can I use .split to split a string AFTER a certain word?

As the title says. 如标题所示。

Let's say we have a string called "value" 假设我们有一个名为“值”的字符串

String value = ("I need to go to the bathroom.");

I want value.split do such a job that it splits the string in half AFTER a certain word. 我想要value.split做这样的工作,即在某个单词之后将字符串分成两半。

So that we have an array of strings that has 2 strings inside. 这样我们就有了一个包含2个字符串的字符串数组。

Such as

String[] valuearray = value.split(-whatevercodegoeshere-);

So we get: 这样我们得到:

valuearray 1 = I need to valuearray 1 =我需要

valuearray 2 = go to the bathroom. valuearray 2 =去洗手间。

Is this possible? 这可能吗? If not with current functions, is it possible by writing a function specifically designed for this task? 如果没有当前功能,是否可以编写专门为此任务设计的功能?

Try using a lookbeind 尝试使用lookbeind

value.split("(?<=go) ")

This reads, split where space is preceded by go. 读取后,在go前面的空格处进行拆分。

Lookaheads and Lookbehinds don't consume anything, only check if it's there, so go will still be in the String[] returned from split() Lookaheads和Lookbehinds不会消耗任何东西,仅检查它是否在那里,所以go仍将存在于split()返回的String []中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM