如何使用一些分隔符拆分字符串但不删除 Java 中的分隔符? [英] How to split String with some separator but without removing that separator in Java?

查看:43
本文介绍了如何使用一些分隔符拆分字符串但不删除 Java 中的分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在拆分 String 时遇到问题.

I'm facing problem in splitting String.

我想用一些分隔符分割一个 String 但不丢失那个分隔符.

I want to split a String with some separator but without losing that separator.

当我们在 Java 中使用 somestring.split(String separator) 方法时,它会拆分 String 但从 String 中删除分隔符部分.我不希望这发生.

When we use somestring.split(String separator) method in Java it splits the String but removes the separator part from String. I don't want this to happen.

我想要如下结果:

String string1="Ram-sita-laxman";
String seperator="-";
string1.split(seperator);

输出:

[Ram, sita, laxman]

但我想要像下面这样的结果:

but I want the result like the one below instead:

[Ram, -sita, -laxman]

有没有办法得到这样的输出?

Is there a way to get output like this?

推荐答案

string1.split("(?=-)");

这是可行的,因为 split 实际上需要一个 正则表达式.您实际看到的是零宽度正前瞻".

This works because split actually takes a regular expression. What you're actually seeing is a "zero-width positive lookahead".

我想解释更多,但我女儿想玩茶话会.:)

I would love to explain more but my daughter wants to play tea party. :)

返回!

为了解释这一点,我将首先向您展示一个不同的split操作:

To explain this, I will first show you a different split operation:

"Ram-sita-laxman".split("");

这会在每个零长度字符串上拆分您的字符串.每个字符之间有一个长度为零的字符串.因此,结果是:

This splits your string on every zero-length string. There is a zero-length string between every character. Therefore, the result is:

["", "R", "a", "m", "-", "s", "i", "t", "a", "-", "l", "a", "x", "m", "a", "n"]

现在,我修改我的正则表达式 ("") 以仅匹配零长度字符串如果它们后跟破折号.

Now, I modify my regular expression ("") to only match zero-length strings if they are followed by a dash.

"Ram-sita-laxman".split("(?=-)");
["Ram", "-sita", "-laxman"]

在该示例中,?= 表示先行".更具体地说,它意味着积极前瞻".为什么是正面"?因为你也可以有 negative 前瞻 (?!),它会分割每个 not 后跟破折号的零长度字符串:

In that example, the ?= means "lookahead". More specifically, it mean "positive lookahead". Why the "positive"? Because you can also have negative lookahead (?!) which will split on every zero-length string that is not followed by a dash:

"Ram-sita-laxman".split("(?!-)");
["", "R", "a", "m-", "s", "i", "t", "a-", "l", "a", "x", "m", "a", "n"]

你也可以有 positive lookbehind (?<=),它将在每个以破折号开头的零长度字符串上进行拆分:

You can also have positive lookbehind (?<=) which will split on every zero-length string that is preceded by a dash:

"Ram-sita-laxman".split("(?<=-)");
["Ram-", "sita-", "laxman"]

最后,您还可以使用否定后视(?<!),它会在每个的零长度字符串上进行拆分前面有一个破折号:

Finally, you can also have negative lookbehind (?<!) which will split on every zero-length string that is not preceded by a dash:

"Ram-sita-laxman".split("(?<!-)");
["", "R", "a", "m", "-s", "i", "t", "a", "-l", "a", "x", "m", "a", "n"]

这四个表达式统称为lookaround表达式.

These four expressions are collectively known as the lookaround expressions.

我只想展示我最近遇到的一个例子,它结合了两个环视表达式.假设您希望将 CapitalCase 标识符拆分为其标记:

I just wanted to show an example I encountered recently that combines two of the lookaround expressions. Suppose you wish to split a CapitalCase identifier up into its tokens:

"MyAwesomeClass" => ["My", "Awesome", "Class"]

您可以使用此正则表达式完成此操作:

You can accomplish this using this regular expression:

"MyAwesomeClass".split("(?<=[a-z])(?=[A-Z])");

这会拆分以小写字母 ((?<=[az])) 开头并后跟大写字母 ((?=[AZ])).

This splits on every zero-length string that is preceded by a lower case letter ((?<=[a-z])) and followed by an upper case letter ((?=[A-Z])).

这种技术也适用于驼峰式标识符.

This technique also works with camelCase identifiers.

这篇关于如何使用一些分隔符拆分字符串但不删除 Java 中的分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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