帮助拆分字符串

时间:2010-11-29 12:11:34

标签: c#

我有字符串数组:

string[] data = new string[] {"1B", "2C", "01", "11", "3F", "5F", "02", "01", "06","12", "1C"};

需要找到“01”,然后找到“01”的下一个元素。复制它们之间的内容,包括第一个“01”等等。我需要获得一个新的数组(或List):

string[] arr;
arr[0] = "01113F5F02";
arr[1] = "0106121C"

OR

List<string> arr;
arr[0] = "01113F5F02";
arr[1] = "0106121C"

感谢。

4 个答案:

答案 0 :(得分:2)

好的,鉴于之前的答案不合适,这是另一次尝试:

public static List<string> SplitAndCombine(IEnumerable<string> source,
                                           string delimiter)
{
    List<string> result = new List<string>();
    StringBuilder current = null;

    // Ignore anything before the first delimiter
    foreach (string item in source.SkipWhile(x => x != delimiter))
    {
        if (item == delimiter)
        {
            if (current != null)
            {
                result.Add(current.ToString());
            }
            current = new StringBuilder();
        }
        current.Append(item);
    } 

    if (current != null)
    {
        result.Add(current.ToString());
    }
    return result;
}

答案 1 :(得分:1)

        string[] input = new string[] { "1B", "2C", "01", "11", "3F", "5F", "02", "01", "06", "12", "1C" };
        string splitStr = "01";
        int firstPos = 0;
        for (int i = 0; i < input.Length; i++)
        {
            if (input[i] == splitStr)
            {
                firstPos = i;
                break;
            }

        }
        var data = input.Skip(firstPos).ToArray() ;

        List<string> result = new List<string>();
        int index = -1;
        string tmp = string.Empty;
        while(++index < data.Length)
        {
            if(data[index] == splitStr && tmp != string.Empty)
            {
                    result.Add(tmp);
                    tmp = string.Empty;
            }
            tmp += data[index];
            if(index == data.Length - 1 && tmp != string.Empty)
                result.Add(tmp);
        }

        //results:
        foreach (var x in result)
        {
            Console.WriteLine(x);
        }

答案 2 :(得分:1)

public static List<string> SplitBy(string[] data, string splitStr)
{
    var res = new List<string>();
    bool passedSplitStr = false;
    var sb = new StringBuilder();
    for(int i = 0; i < data.Length; ++i)
    {
        if(data[i] == splitStr)
        {
            if(!passedSplitStr)
            {
                passedSplitStr = true;
            }
            else
            {
                res.Add(sb.ToString());
                sb.Clear();
            }
            sb.Append(data[i]);
        }
        else
        {
            if(passedSplitStr)
                sb.Append(data[i]);
        }
    }
    if(sb.Length != 0)
        res.Add(sb.ToString());
    return res;
}

用法:

var arr = SplitBy(data, "01");

答案 3 :(得分:1)

string[] data = new string[] { "1B", "2C", "01", "11", "3F", "5F", "02", "01", "06", "12", "1C" };
var res = string.Join(" ", data).Split(new string[] {" 01 "}, StringSplitOptions.RemoveEmptyEntries).Skip(1).Select(x=>"01" + x.Replace(" ","") ).ToArray();