简体   繁体   English

C#中的特殊字符串拆分

[英]Special String splitting in C#

I would like to split a special formatted string : 我想分割一个特殊格式的string

string example01 = "2016-05-14-Title-863-Pos-01"

That string must be split into: string必须分为:

date = "2016-05-14";
title = "Title-863";
position = "Pos-01";

I know I can split this string with string.split or Regex but I would like to have a "split" function with formatted seperators or something similar. 我知道我可以使用string.splitRegex拆分此string ,但是我想使用格式化分隔符或类似的东西来实现“拆分”功能。

More examples: 更多示例:

string example02 = "2016-05-15-Rectangle-Right"
string example03 = "2016-05-16-Border-05-Top"

Example-Method: 示例方法:

string[] split = SplitString("****-**-**", "-");

So the first parameter serves as a placeholder and the second parameter as seperator. 因此,第一个参数用作占位符,第二个参数用作分隔符。

You could use a generic BlockSplit method like this one 您可以使用像这样的通用BlockSplit方法

string[] BlockSplit(string source, char separator, params int[] blocks)
{
    string[] parts = source.Split(separator);
    List<string> splitted = new List<string>();

    int skipped = 0;
    foreach (int x in blocks)
    {
        string block = string.Join(separator.ToString(), parts.Skip(skipped).Take(x));
        skipped += x;
        splitted.Add(block);
    }
    return splitted.ToArray();
}

Calling this method with your inputs 用您的输入调用此方法

string test = "2016-05-14-Title-863-Pos-01";
string[] result = BlockSplit(test, '-', new int[] {3,2,2});
foreach(string s in result)
   Console.WriteLine(s);

This could transformed in an extension method for the string class and and allow a syntax like this one 这可以转换为字符串类的扩展方法 ,并允许使用这种语法

string test = "2016-05-14-Title-863-Pos-01";
string[] result = test.BlockSplit('-', new int[] {3,2,2});

This only works if the string is always in that exact format. 仅当字符串始终使用该确切格式时,此方法才有效。 If anything changes this becomes flakey: 如果发生任何变化,这将变得容易:

// Original string, has to be in the exact format shown
var test = "2016-05-14-Title-863-Pos-01";

// Split into groups based on the '-' character
var split = test.Split('-');

// Uses string interpolation '$' to build new strings
var date = $"{split[0]}-{split[1]}-{split[2]}";
var title = $"{split[3]}-{split[4]}";
var position $"{split[5]}-{split[6]}";

Output: 输出:

Date: 2016-05-14 日期:2016-05-14
Title: Title-863 标题:Title-863
Position: Pos-01 位置:Pos-01

As pointed out by Phiter, if using an older version if C# then use: 正如Phiter指出的那样,如果使用的是较旧版本的C#,请使用:

var date = string.Format("{0}-{1}-{2}", split[0], split[1], split[2]);
var title = split[3] + "-" + split[4];
var position = split[5] + "-" + split[6];   

If your data format will always be the same, you can do it like this: 如果您的数据格式始终相同,则可以这样操作:

string test = "2016-05-14-Title-863-Pos-01";
string[] ts = test.Split('-');
string date = ts[0] + "-" + ts[1] + "-" + ts[2];
string title = ts[3] + "-" + ts[4];
string position = ts[5] + "-" + ts[6];

Console.WriteLine(String.Format("Date: {0}\nTitle: {1}\nPosition: {2}", date,title,position));

Output: 输出:

Date: 2016-05-14 日期:2016-05-14
Title: Title-863 标题:Title-863
Position: Pos-01 位置:Pos-01

See it in action. 看到它在行动。

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

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