开发者

Help split the string

I have the string array:

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

Need to find a "01" and then find the next element of the "01". Copying what is between them, including the first "01", and etc. I need to get a new array(or List):

string[] arr;
arr[0] = "01113F5F02";
arr[1]开发者_如何学JAVA = "0106121C"

OR

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

Thanks.


Okay, given that the previous answer wasn't appropriate, here's another attempt:

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;
}


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;
}

Usage:

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


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();


        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);
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜