日期:2014-05-18  浏览次数:21361 次

string的匹配与截取
有一个字符串,包含若干个回车符"\r\n"
当回车符"\r\n"的个数超过10个时,就把该string在第10个回车符的地方,截取为两个string.

------解决方案--------------------
探讨
有一个字符串,包含若干个回车符"\r\n"
当回车符"\r\n"的个数超过10个时,就把该string在第10个回车符的地方,截取为两个string.

------解决方案--------------------
C# code

string s = "aaa\r\nbb\r\ncc\r\n";
            string [] strs =s.Split(new string[]{"\r\n"},StringSplitOptions.None);
             s = string.Empty;
string s1 = string.Empty;
            for (int i = 0; i < strs.Length; i++)
            {
                if (i < 10)
                {
                    s += strs[i] + "\r\n";
                }
                else
                {
                    s1+strs[i]+"\r\n";                }
            }
            Console.WriteLine(s);
Console.WriteLine(s1);

------解决方案--------------------
足10则为一组...
C# code

            String str = "abc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\nabc\r\n";

            String[] temp = str.Split(new String[]{"\r\n"},StringSplitOptions.None);
            List<String> list = new List<String>();
            String tempStr = "";

            for (Int32 i = 0; i < temp.Length; i++)
            {
                if (i % 10 == 9)
                {
                    list.Add(tempStr);
                    tempStr = "";
                }
                else
                {
                    tempStr += temp[i] + "\r\n";
                }
            }

            foreach (String s in list)
            {
                Console.WriteLine(s);
                Console.WriteLine();
                Console.WriteLine();
            }

------解决方案--------------------
C# code

public class Test
    {
        public static void Main(string[] args)
        {
            string str = "asdf\r\njdfd\r\nfaa\r\nasd\r\ndf\r\nfa\r\nsas\r\ndfas\r\ndfsdf\r\naasdf\r\nasdf\r\nsdio\r\nghhsd\r\nfgsg\r\nfgfgasfafasddgfjoifsdf";
            int count = 10, indexOf = -1;
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] == '\r' && i != str.Length - 1 && str[i + 1] == '\n')
                    count--;

                if (count == 0)
                {
                    indexOf = i;
                    break;
                }
            }
            string subStr1 = str.Substring(0,indexOf);
            string subStr2 = str.Substring(indexOf);

            Console.WriteLine(subStr1 + " : " + subStr2);
            Console.ReadLine();
        }
    }

------解决方案--------------------
C# code

string str="sfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\rsfdsfs\n\r";
            Regex reg=new Regex(@"\n\r");
            MatchCollection mc=reg.Matches(str);
            int num=0;
            string str2=String.Empty;
            foreach( Match m in mc )
            {
                num++;
                if(num==10)
                {
                    str2=str.Substring(m.Index);
                }
            }