简体   繁体   English

在字符后用空格/两个空格拆分字符串

[英]Splitting a string with a space/two spaces after the character

Consider a number of strings, which are assumed to contain "keys" of the form "Wxxx", where x are digits from 0-9.考虑一些字符串,假设它们包含“Wxxx”形式的“键”,其中 x 是 0-9 的数字。 Each one can contain either one only, or multiple ones, separated by ',' followed by two spaces.每一个都可以只包含一个,也可以包含多个,用“,”隔开,后跟两个空格。 For example:例如:

W123
W432
W546,  W234,  W167

The ones that contain multiple "keys" need to be split up, into an array.包含多个“键”的那些需要被拆分成一个数组。 So, the last one in the above examples should be split into an array like this: {"W546", "W234", "W167"} .所以,上面例子中的最后一个应该被拆分成这样的数组: {"W546", "W234", "W167"}

As a quick solution, String.Split comes to mind, but as far as I am aware, it can take one character, like ','.作为一种快速解决方案,我String.Split ,但据我所知,它可以采用一个字符,例如 ','。 The problem is that it would return an array with like this: {"W546", " W234", " W167"} .问题是它会返回一个这样的数组: {"W546", " W234", " W167"} The two spaces in all the array entries from the second one onwards can probably be removed using Substring , but is there a better solution?可以使用Substring删除从第二个开始的所有数组条目中的两个空格,但是有更好的解决方案吗?

For context, these values are being held in a spreadsheet, and are assumed to have undergone data validation to ensure the "keys" are separated by a comma followed by two spaces.就上下文而言,这些值保存在电子表格中,并假定已经过数据验证以确保“键”由逗号后跟两个空格分隔。

while ((ws.Cells[row,1].Value!=null) && (ws.Cells[row,1].Value.ToString().Equals("")))
{
    // there can be one key, or multiple keys separated by ','
    if (ws.Cells[row,keysCol].Value.ToString().Contains(','))
    {
        // there are multiple
        // need to split the ones in this cell separated by a comma           
    }
    else
    {
        // there is one
    }

    row++;
}

You can just specify ',' and ' ' as separators and RemoveEmptyEntries .您可以只指定','' '作为分隔符和RemoveEmptyEntries

Using your sample of single keys and a string containing multiple keys you can just handle them all the same and get your list of individual keys:使用您的单键示例和包含多个键的字符串,您可以完全相同地处理它们并获取单个键的列表:

List<string> cells = new List<string>() { "W123", "W432", "W546,  W234,  W167" };
List<string> keys = new List<string>();

foreach (string cell in cells)
{
    keys.AddRange(cell.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries));
}

Split can handle strings where's nothing to split and AddRange will accept your single keys as well as the multi-key split results. Split可以处理无需拆分的字符串, AddRange将接受您的单键以及多键拆分结果。

Eliminate the extra space first (using Replace() ), then use split.首先消除多余的空间(使用Replace() ),然后使用 split。

var input = "W546, W234, W167";
var normalized = input.Replace(", ",",");  
var array = normalized.Split(',');

This way, you treat a comma followed by a space exactly the same as you'd treat a comma.这样,您处理逗号后跟一个空格的方式与处理逗号的方式完全相同。 If there might be two spaces you can also replace that:如果可能有两个空格,您也可以替换它:

var input = "W546,  W234, W167";
var normalized = input.Replace("  "," ").Replace(", ",",");  
var array = normalized.Split(',');

You could use an old favorite--Regular Expressions.您可以使用旧的最爱——正则表达式。

Here are two flavors 'Loop' or 'LINQ'.这里有两种口味“Loop”或“LINQ”。

    static void Main(string[] args)
    {
        var list = new List<string>{"W848","W998, W748","W953, W9484, W7373","W888"};

        Console.WriteLine("LINQ");
        list.ForEach(l => TestSplitRegexLinq(l));

        Console.WriteLine();
        Console.WriteLine("Loop");
        list.ForEach(l => TestSplitRegexLoop(l));
    }


    private static void TestSplitRegexLinq(string s)
    {
        string pattern = @"[W][0-9]*";                
        var reg = new Regex(pattern);
        reg.Matches(s).ToList().ForEach(m => Console.WriteLine(m.Value));
    }



    private static void TestSplitRegexLoop(string s)
    {
        string pattern = @"[W][0-9]*";                
        var reg = new Regex(pattern);
        foreach (Match m in reg.Matches(s))
        {
            Console.WriteLine(m.Value);
        }
    }

Just replace the Console.Write with anything you want: eg.只需将Console.Write替换为您想要的任何内容:例如。 myList.Add(m.Value) . myList.Add(m.Value)

You will need to add the NameSpace: using System.Text.RegularExpressions;您将需要添加 NameSpace: using System.Text.RegularExpressions;

After trying this in .NET fiddle, I think I may have a solution:在 .NET fiddle 中尝试这个之后,我想我可能有一个解决方案:

// if there are multiple
string keys = ws.Cells[row,keysCol].Value.ToString();

// remove spaces
string keys_normalised = keys.Replace(" ", string.Empty);
Console.WriteLine("Checking that spaces have been removed: " + keys3_normalised + "\n");

string[] splits = keys3_normalised.Split(',');
for (int i = 0; i < splits.Length; i++)
{
    Console.WriteLine(splits[i]);
}

This produces the following output in the console:这会在控制台中产生以下输出:

Checking that spaces have been removed: W456,W234,W167

W456
W234
W167

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

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