• 切分文本行


    /// <summary>
            /// 切分文本行
            /// </summary>
            /// <param name="src">源内容</param>
            /// <param name="separators">分隔符</param>
            /// <param name="quotes">引号对</param>
            /// <returns></returns>
            public string[] cutTextLine(string src, string[] separators, string[][] quotes)
            {
                if (src == null || src.Equals("")) { return new string[0]; }
                ArrayList als = new ArrayList();
                int point = 0;
                while (point < src.Length)
                {
                    while (point < src.Length && src.Substring(point, 1).Equals(" ")) { point++; }  // 过滤前空
                    int q_point = -1;    // 引号位
                    for (int i = 0; i < quotes.Length; i++)
                    {                    // 以左引号打头
                        if (point + quotes[i][0].Length <= src.Length && quotes[i][0].Equals(src.Substring(point, quotes[i][0].Length)))
                        {
                            q_point = i;
                            break;
                        }
                    }
                    if (q_point < 0)   // 非引号打头
                    {
                        int k = point;
                        int s_point = -1;    // 分隔符位
                        while (k < src.Length && s_point < 0)
                        {
                            for (int i = 0; i < separators.Length; i++)            // 找分隔符
                            {
                                if (k + separators[i].Length <= src.Length && separators[i].Equals(src.Substring(k, separators[i].Length)))
                                {
                                    s_point = i;
                                    break;
                                }
                            }
                            if (s_point < 0) { k++; }
                        }
                        string s = (point < k ? src.Substring(point, k - point) : "");
                        als.Add(s);
                        point = (s_point < 0 ? k : k + separators[s_point].Length);
                    }
                    else
                    {
                        point += quotes[q_point][0].Length;
                        int k = src.IndexOf(quotes[q_point][1], point);
                        if (k < 0 || k >= src.Length)     // 没找到后续右引号
                        {
                            string s = src.Substring(point);
                            als.Add(s);
                            point = src.Length;
                        }
                        else
                        {
                            string s = src.Substring(point, k - point);
                            als.Add(s);
                            point = k + quotes[q_point][1].Length;
                        }
                        while (point < src.Length && src.Substring(point, 1).Equals(" ")) { point++; }  // 过滤后空
                        for (int i = 0; i < separators.Length; i++)           // 跳过分隔符
                        {
                            if (point + separators[i].Length <= src.Length && separators[i].Equals(src.Substring(point, separators[i].Length)))
                            {
                                point += separators[i].Length;
                                break;
                            }
                        }
                    }
                }
                string[] ss = new string[als.Count];
                for (int i = 0; i < als.Count; i++) { ss[i] = (string)als[i]; }
                als.Clear();
                return ss;
            }
  • 相关阅读:
    software系列:Tableau10.5安装教程(破解版)
    技巧系列:Excel打印技巧
    Android 3.0 访问WebService 出现 android.os.NetworkOnMainThreadException异常
    Android各种布局
    服务器访问本地磁盘
    Android手机客户端访问.NET服务器端的方法(1)
    flex webservice
    android学习之 intent 实例
    Android手机客户端访问.NET服务器端的方法(2)
    一篇不错的讲解Java异常的文章(转载)感觉很不错,读了以后很有启发
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1951804.html
Copyright © 2020-2023  润新知