我想用","分割字符串。但是从第二个逗号(,)

时间:2016-04-07 14:28:07

标签: c#

我想用","分割字符串但是从第二个逗号(,)

6 个答案:

答案 0 :(得分:2)

没有尝试过,但你需要这样的东西:

string pattern = "|\*,";            
string[] result = Regex.Split(before_split, pattern);

答案 1 :(得分:0)

如果您正在寻找先跳过',' &安培;然后通过','来分割其余的字符串。然后你可以试试。

string before_split = "pune,mumbai|01234,delhi|65432,Bhopal|09231,jabalpur|0987765";

var firstPart = before_split.Substring(0, before_split.IndexOf(",", System.StringComparison.Ordinal));
var stringToProcess = before_split.Substring(before_split.IndexOf(",", System.StringComparison.Ordinal) + 1);
var stringSegments = stringToProcess.Split(',');

Console.WriteLine("{0},{1}",firstPart ,stringSegments[0]);

for (var i = 1; i < stringSegments.Length; i++)
{
    Console.WriteLine(stringSegments[i]);
}

答案 2 :(得分:0)

试试这个,最终结果是在List finalSplit

            ListItem item = new ListItem();
            item.Text = row["Previlage_pageName"].ToString();
            item.Value = row["Previliges_ID"].ToString();
            item.Selected = true;
            CHpagelist.Items.Add(item);

希望有所帮助

答案 3 :(得分:0)

我的解决方案是

string before_split = "pune,mumbai|01234,delhi|65432,Bhopal|09231,123,jabalpur|0987765";

string buffer = "";
var parts = before_split.Split(',');
var lines = parts.Select(p =>
{
    if (p.Contains('|'))
    {
        var line = buffer == "" ? p : buffer + ',' + p;
        buffer = "";
        return line;
    }
    else
    {
        buffer = buffer == "" ? p : buffer + ',' + p;
        return null;
    }
}).Where(p => p != null).ToArray(); 

答案 4 :(得分:0)

这个怎么样......

        Regex rex = new Regex("[a-zA-Z]+[a-zA-Z,]*[|]+[0-9]+");

        var result = rex.Matches("pune,mumbai|01234,delhi|65432,Bhopal|09231,jabalpur|0987765").Cast<Match>()
            .Select(m => m.Value)
            .ToArray()

答案 5 :(得分:-2)

var result =before_split.Split(',')