如何使用一些分隔符拆分字符串但不删除 Java 中的分隔符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4416425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split String with some separator but without removing that separator in Java?
提问by sag
I'm facing problem in splitting String.
我在拆分时遇到问题String。
I want to split a Stringwith some separator but without losing that separator.
我想String用一些分隔符拆分 a但不丢失该分隔符。
When we use somestring.split(String separator)method in Java it splits the Stringbut removes the separator part from String. I don't want this to happen.
当我们somestring.split(String separator)在 Java 中使用method 时,它会拆分 ,String但会从 中删除分隔符部分String。我不希望这种情况发生。
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?
有没有办法得到这样的输出?
回答by Dalmas
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.
一种方法是拆分您的字符串,然后在每个提取的字符串的开头添加分隔符,但第一个除外。
回答by Andrew Cooper
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.
当然,您需要选择一个虚拟分隔符,保证不会出现在字符串中的任何其他地方。
回答by Adam Paynter
string1.split("(?=-)");
This works because splitactually 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 splitoperation:
为了解释这一点,我将首先向您展示一个不同的split操作:
"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.
现在,我修改我的正则表达式 ( "") 以仅匹配零长度字符串,如果它们后跟一个 dash。
"Ram-sita-laxman".split("(?=-)");
["Ram", "-sita", "-laxman"]
In that example, the ?=means "lookahead". More specifically, it mean "positivelookahead". Why the "positive"? Because you can also have negativelookahead (?!) which will split on every zero-length string that is notfollowed 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:
您还可以使用正向后视( ?<=),它会在每个以破折号开头的零长度字符串上进行拆分:
"Ram-sita-laxman".split("(?<=-)");
["Ram-", "sita-", "laxman"]
Finally, you can also have negative lookbehind(?<!) which will split on every zero-length string that is notpreceded by a dash:
最后,您还可以使用负向后视( ?<!) ,它将在每个前面没有破折号的零长度字符串上进行拆分:
"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 lookaroundexpressions.
这四个表达式统称为环视表达式。
Bonus: Putting them together
奖励:把它们放在一起
I just wanted to show an example I encountered recently that combines twoof the lookaround expressions. Suppose you wish to split a CapitalCase identifier up into its tokens:
我只是想展示一个我最近遇到的例子,它结合了两个环视表达式。假设您希望将 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.
这种技术也适用于驼峰式标识符。
回答by mad
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.
这是适合 LadaRaider 答案的代码。
回答by Walrusking
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.
亚当一针见血!我用他的回答来弄清楚如何将文件对话框浏览器中的文件名文本插入到富文本框中。我遇到的问题是当我在文件字符串的“\”处添加新行时。string.split 命令在 \ 处拆分并删除它。在混合使用 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
海象

