• 字符串的分割操作


     static void Main(string[] args)
            {
                //重复分割符问题的实践和演示
                //实践运用的场景||、|同为分割符,2替换为空时出现的|||情况
                string content = "1234567890";
                string replaceOptions = "1|a||2|||3|||4|c";
                Console.WriteLine(replaceContent(content, replaceOptions));
                Console.ReadKey();
            }
     /**
             * 适用于1|a||2|||3|c模式分割,兼容多个|||的情况
             **/
            public static string replaceContent(string content, string filterWord)
            {
                ArrayList filterWhiteSpace = new ArrayList();
                spliteMuliSymbol(filterWord, ref filterWhiteSpace);
                for (int i = 0; i < filterWhiteSpace.Count; i++)
                {
                    string[] replaceArr = filterWhiteSpace[i].ToString().Split('|');
                    content = Regex.Replace(content, replaceArr[0], replaceArr[1]);
                }
                return content;
            }
     public static void spliteMuliSymbol(string filterWord, ref ArrayList filterWhiteSpace)
            {
                //判断是否存在替换为空的情况
                if (filterWord.Contains("|||"))
                {
                    int whiteSpaceIndex = filterWord.IndexOf("|||");
                    ArrayList arrHalf = new ArrayList();
                    string firstHalf = filterWord.Substring(0, whiteSpaceIndex + 1);
                    string latterHalf = filterWord.Substring(whiteSpaceIndex + 3);
                    arrHalf.Add(firstHalf);
                    arrHalf.Add(latterHalf);
                    for (int i = 0; i < arrHalf.Count; i++)
                    {
                        spliteMuliSymbol(arrHalf[i].ToString(), ref filterWhiteSpace);
                    }
                }
                else
                {
                    string[] filterWordArray = Regex.Split(filterWord, @"||");
                    foreach (string temp in filterWordArray)
                    {
                        filterWhiteSpace.Add(temp);
                    }
                }
            }
  • 相关阅读:
    洛谷 P1430 序列取数
    洛谷 P2042 维护数列
    洛谷 P3391 【模板】文艺平衡树(Splay)
    Permutation UVA
    treap板子(洛谷 P3369 【模板】普通平衡树(Treap/SBT))
    Jumping Jack CodeForces
    Increasing Sequence CodeForces
    Cunning Gena CodeForces
    Hie with the Pie POJ
    ACboy needs your help HDU
  • 原文地址:https://www.cnblogs.com/meiCode/p/4871521.html
Copyright © 2020-2023  润新知