This question already has an answer here:
这个问题已经有了答案:
- How to split a string, but also keep the delimiters? 23 answers
- 如何分割一个字符串,同时还要保留分隔符?23日答案
I'm facing problem in splitting String
.
我正面临分裂弦的问题。
I want to split a String
with some separator but without losing that separator.
我想用一些分隔符来分割一个字符串,但不会丢失这个分隔符。
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.
当我们使用somestring。分割(字符串分隔符)方法在Java中,它分割字符串,但从字符串中删除分隔符部分。我不希望这种事发生。
I want result like below:
我想要的结果如下:
String string1="Ram-sita-laxman";
String seperator="-";
string1.split(seperator);
Output:
输出:
[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?
有办法得到这样的输出吗?
5 个解决方案
#1
168
string1.split("(?=-)");
This works because split
actually takes a regular expression. What you're actually seeing is a "zero-width positive lookahead".
这是可行的,因为split实际上接受一个正则表达式。你实际上看到的是一个“零宽度的正面展望”。
I would love to explain more but my daughter wants to play tea party. :)
我很想多解释一下,但我女儿想参加茶会。:)
Edit: Back!
编辑:回来了!
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"]
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"]
You can also have positive lookbehind (?<=
) which will split on every zero-length string that is preceded by a dash:
您也可以有一个正的lookbehind(?<=),它将在每一个零长度的字符串前面加上一个破折号:
"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:
最后,您还可以有negative lookbehind (?
"Ram-sita-laxman".split("(?<!-)");
["", "R", "a", "m", "-s", "i", "t", "a", "-l", "a", "x", "m", "a", "n"]
These four expressions are collectively known as the lookaround expressions.
这四个表达式统称为lookaround表达式。
Bonus: Putting them together
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:
我只是想展示我最近遇到的一个例子,它结合了两个lookaround表达式。假设您希望将CapitalCase标识符拆分为其令牌:
"MyAwesomeClass" => ["My", "Awesome", "Class"]
You can accomplish this using this regular expression:
您可以使用这个正则表达式来完成:
"MyAwesomeClass".split("(?<=[a-z])(?=[A-Z])");
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])
).
这将分割每个长度为零的字符串,该字符串前面有一个小写字母(?<=[a-z]),后面跟着一个大写字母(?=[a-z])。
This technique also works with camelCase identifiers.
这种技术也适用于camelCase标识符。
#2
4
It's a bit dodgy, but you could introduce a dummy separator using a replace function. I don't know the Java methods, but in C# it could be something like:
这有点麻烦,但是您可以使用替换函数引入一个虚拟分隔符。我不知道Java方法,但是在c#中,它可以是:
string1.Replace("-", "#-").Split("#");
Of course, you'd need to pick a dummy separator that's guaranteed not to be anywhere else in the string.
当然,您需要选择一个虚拟分隔符,它保证不会出现在字符串中的其他位置。
#3
2
A way to do this is to split your string, then add your separator at the beginning of each extracted string except the first one.
一种方法是分割字符串,然后在每个提取的字符串的开头添加分隔符,第一个字符串除外。
#4
1
seperator="-";
String[] splitstrings = string1.split(seperator);
for(int i=1; i<splitstring.length;i++)
{
splitstring[i] = seperator + splitstring[i];
}
that is the code fitting to LadaRaider's answer.
这就是拉达莱德的答案。
#5
0
Adam hit the nail on the head! I used his answer to figure out how to insert filename text from the file dialog browser into a rich text box. The problem I ran into was when I was adding a new line at the "\" in the file string. The string.split command was splitting at the \ and deleting it. After using a mixture of Adam's code I was able to create a new line after each \ in the file name.
亚当击中了要害!我用他的回答找出如何将文件对话框浏览器中的文件名文本插入到富文本框中。我遇到的问题是,当我在文件字符串的“\”中添加一个新行时。的字符串。分割命令在\和删除它。在使用了Adam的混合代码之后,我能够在文件名中每个\后面创建一个新的行。
Here is the code I used:
下面是我使用的代码:
OpenFileDialog fd = new OpenFileDialog();
fd.Multiselect = true;
fd.ShowDialog();
foreach (string filename in fd.FileNames)
{
string currentfiles = uxFiles.Text;
string value = "\r\n" + filename;
//This line allows the Regex command to split after each \ in the filename.
string[] lines = Regex.Split(value, @"(?<=\\)");
foreach (string line in lines)
{
uxFiles.Text = uxFiles.Text + line + "\r\n";
}
}
Enjoy!
享受吧!
Walrusking
Walrusking
#1
168
string1.split("(?=-)");
This works because split
actually takes a regular expression. What you're actually seeing is a "zero-width positive lookahead".
这是可行的,因为split实际上接受一个正则表达式。你实际上看到的是一个“零宽度的正面展望”。
I would love to explain more but my daughter wants to play tea party. :)
我很想多解释一下,但我女儿想参加茶会。:)
Edit: Back!
编辑:回来了!
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"]
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"]
You can also have positive lookbehind (?<=
) which will split on every zero-length string that is preceded by a dash:
您也可以有一个正的lookbehind(?<=),它将在每一个零长度的字符串前面加上一个破折号:
"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:
最后,您还可以有negative lookbehind (?
"Ram-sita-laxman".split("(?<!-)");
["", "R", "a", "m", "-s", "i", "t", "a", "-l", "a", "x", "m", "a", "n"]
These four expressions are collectively known as the lookaround expressions.
这四个表达式统称为lookaround表达式。
Bonus: Putting them together
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:
我只是想展示我最近遇到的一个例子,它结合了两个lookaround表达式。假设您希望将CapitalCase标识符拆分为其令牌:
"MyAwesomeClass" => ["My", "Awesome", "Class"]
You can accomplish this using this regular expression:
您可以使用这个正则表达式来完成:
"MyAwesomeClass".split("(?<=[a-z])(?=[A-Z])");
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])
).
这将分割每个长度为零的字符串,该字符串前面有一个小写字母(?<=[a-z]),后面跟着一个大写字母(?=[a-z])。
This technique also works with camelCase identifiers.
这种技术也适用于camelCase标识符。
#2
4
It's a bit dodgy, but you could introduce a dummy separator using a replace function. I don't know the Java methods, but in C# it could be something like:
这有点麻烦,但是您可以使用替换函数引入一个虚拟分隔符。我不知道Java方法,但是在c#中,它可以是:
string1.Replace("-", "#-").Split("#");
Of course, you'd need to pick a dummy separator that's guaranteed not to be anywhere else in the string.
当然,您需要选择一个虚拟分隔符,它保证不会出现在字符串中的其他位置。
#3
2
A way to do this is to split your string, then add your separator at the beginning of each extracted string except the first one.
一种方法是分割字符串,然后在每个提取的字符串的开头添加分隔符,第一个字符串除外。
#4
1
seperator="-";
String[] splitstrings = string1.split(seperator);
for(int i=1; i<splitstring.length;i++)
{
splitstring[i] = seperator + splitstring[i];
}
that is the code fitting to LadaRaider's answer.
这就是拉达莱德的答案。
#5
0
Adam hit the nail on the head! I used his answer to figure out how to insert filename text from the file dialog browser into a rich text box. The problem I ran into was when I was adding a new line at the "\" in the file string. The string.split command was splitting at the \ and deleting it. After using a mixture of Adam's code I was able to create a new line after each \ in the file name.
亚当击中了要害!我用他的回答找出如何将文件对话框浏览器中的文件名文本插入到富文本框中。我遇到的问题是,当我在文件字符串的“\”中添加一个新行时。的字符串。分割命令在\和删除它。在使用了Adam的混合代码之后,我能够在文件名中每个\后面创建一个新的行。
Here is the code I used:
下面是我使用的代码:
OpenFileDialog fd = new OpenFileDialog();
fd.Multiselect = true;
fd.ShowDialog();
foreach (string filename in fd.FileNames)
{
string currentfiles = uxFiles.Text;
string value = "\r\n" + filename;
//This line allows the Regex command to split after each \ in the filename.
string[] lines = Regex.Split(value, @"(?<=\\)");
foreach (string line in lines)
{
uxFiles.Text = uxFiles.Text + line + "\r\n";
}
}
Enjoy!
享受吧!
Walrusking
Walrusking