简体   繁体   English

基于最后N个分隔符分割字符串

[英]Split string base on the last N numbers of delimiters

I need help to develop a logic to split a string, but only based on the last 2 delimiters of the string. 我需要帮助来开发一个逻辑来分割字符串,但只是基于字符串的最后2个分隔符。

Example inputs: 示例输入:

string s1 = "Dog \ Cat \ Bird \ Cow";

string s2 = "Hello \ World \ How \ Are \ You";

string s3 = "I \ am \ Peter";

Expected Outputs: 预期产出:

string[] newS1 = "Dog Cat", "Bird", "Cow"
string[] newS2 = "Hello World How", "Are", "You"
string[] newS3 = "I", "am", "Peter"

So, as you can see, I only want to split the string on the last 2 "\\", and everything else before the last 2 "\\" will be concatenated into one string. 所以,正如你所看到的,我只想将字符串拆分为最后2个“\\”,而最后2个“\\”之前的所有其他内容将被连接成一个字符串。

I tried the .Split method but it will just split every "\\" in a string. 我尝试了.Split方法,但它只是将字符串中的每个“\\”分开。

Edited: If the string has less than 2 "\\", it will just split according to whatever it has 编辑:如果字符串少于2“\\”,它将根据它具有的任何内容进行分割

Updates: Wow, these are a bunch of interesting solutions! 更新:哇,这些都是一堆有趣的解决方案! Thank you a lot! 非常感谢!

Try this: 尝试这个:

var parts = s1.Split(new[] { " \\ " }, StringSplitOptions.None);
var partsCount = parts.Count();
var result = new[] { string.Join(" ", parts.Take(partsCount - 2)) }.Concat(parts.Skip(partsCount - 2));

Offering a regex solution: 提供正则表达式解决方案:

var output = Regex.Split(input, @"\s*\\\s*([^\\]*?)\s*\\\s*(?=[^\\]*$)");

This split finds the second to last element and splits around that, but captures it in a group so it will be included in the output array. 此拆分找到倒数第二个元素并将其拆分,但将其捕获到一个组中,以便它将包含在输出数组中。

For input "Dog \\ Cat \\ Bird \\ Cow" , this will produce { "Dog \\ Cat", "Bird", "Cow" } . 对于输入"Dog \\ Cat \\ Bird \\ Cow" ,这将产生{ "Dog \\ Cat", "Bird", "Cow" } If you also need to strip the \\ out of the first element that can be done with a simple replace: 如果你还需要从第一个元素中删除\\ ,可以通过简单的替换来完成:

output[0] = output[0].Replace(" \\", "");

Update : This version will correctly handle strings with only one delimiter: 更新 :此版本将只使用一个分隔符正确处理字符串:

var output = Regex.Split(str, @"\s*\\\s*([^\\]*?)\s*\\\s*(?=[^\\]*$)|(?<=^[^\\\s]*)\s*\\\s*(?=[^\\\s]*$)");

Update : And to match other delimiters like whitespace, "~" , and "%" , you can use a character class : 更新 :要匹配其他分隔符,如空格, "~""%" ,您可以使用字符类

var output = Regex.Split(str, @"(?:[%~\s\\]+([^%~\s\\]+?)[%~\s\\]+|(?<=^[^%~\s\\]+)[%~\s\\]+)(?=[^%~\s\\]+$)");

The structure of this regex is slightly simpler than the previous one since it represents any sequence of one or more characters in the class [%~\\s\\\\] as a delimiter, and any sequence of one or more characters in the negated character class [^%~\\s\\\\] to be a segment. 这个正则表达式的结构比前一个更简单,因为它表示类[%~\\s\\\\]作为分隔符的一个或多个字符的任何序列,以及否定字符类中的一个或多个字符的任何序列[^%~\\s\\\\]成为一个细分。 Note that the \\s means 'whitespace' character . 请注意, \\s表示'空格'字符

You might also be able to simplify this further using: 您也可以使用以下方法进一步简化:

var output = Regex.Split(str, @"(?:\W+(\w+)\W+|(?<=^\w+)\W+)(?=\w+$)");

Where \\w matches any 'word' character (letters, digits, or underscores) and \\W matches any 'non-word' character . \\w匹配任何'单词'字符 (字母,数字或下划线)和\\W匹配任何'非单词'字符

Looks like you want to Split the string on every <space>\\<space> : 看起来你想在每个<space>\\<space>Split字符串:

string input = @"Dog \ Cat \ Bird \ Cow";
string[] parts = input.Split(new string[]{@" \ "}, 
    StringSplitOptions.None);

And then Join everything with a space in between, except the final two parts: 然后Join所有内容之间的空间,除了最后两部分:

// NOTE: Check that there are at least 2 parts.
string part0 = String.Join(" ", parts.Take(parts.Length - 2));
string part1 = parts[parts.Length - 2];
string part2 = parts[parts.Length - 1];

This will give you three strings, which you can put in an array. 这将为您提供三个字符串,您可以将其放入数组中。

string[] newParts = new []{ part0, part1, part2 };

In this example: 在这个例子中:

new [] { "Dog Cat", "Bird", "Cow" }

How about simply taking the output of split, then taking first N-2 items and Join back together, then create new string array of 3 items, first being output of Join, second being item N-1 of first split, and third being N of first split. 如何简单地获取split的输出,然后取第一个N-2项并一起加入,然后创建3个项的新字符串数组,首先输出Join,第二个是第一个分割的项N-1,第三个是N第一次分裂。 I think that'll accomplish what you're trying to do. 我认为这将完成你想要做的事情。

Interesting question. 有趣的问题。 My initial solution to this would be: 我最初的解决方案是:

String[] tokens = theString.Split("\\");
String[] components = new String[3];
for(int i = 0; i < tokens.length - 2; i++)
{
    components[0] += tokens[i];
}

components[1] = tokens[tokens.length - 2];
components[2] = tokens[tokens.length - 1];

Loop from the end of the string and count delimiters until you encounter two. 从字符串的末尾循环并计算分隔符,直到遇到两个。 Record index positions in 2 variables previously set to -1. 在先前设置为-1的2个变量中记录索引位置。

After the loop, if first var is -1, nothing happens, return whole string. 在循环之后,如果第一个var是-1,则没有任何反应,返回整个字符串。

If second var is -1, create array of 2 strings, split using substring and return. 如果第二个var为-1,则创建包含2个字符串的数组,使用substring和return进行拆分。

Create array of 3 string, split using information from two vars, return. 创建3个字符串的数组,使用来自两个vars的信息进行拆分,返回。

Hope you understood my pseudocode, give me a comment if you need help. 希望你理解我的伪代码,如果你需要帮助,请给我评论。

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

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