一不规则字符串(全汉字),要求每30个汉字截断,使用循环输出被截断的每个字符串,
若用SUBSTRING则最后一字符串可能出错,
要考虑程序运行速度,不允许使用异常处理。

解决方案 »

  1.   

    SUBSTRING前加正则表达式判断,不够30位就不要截取了.
      

  2.   

    先算总长度再算出循环多少次再算最后一个字符串SUBSTRING时应该取多少位?总感觉有点复杂
      

  3.   

                string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";
                string[] strarr = Regex.Replace(str, @"[\u4e00-\u9fa5]{30}", @"$&~").TrimEnd('~').Split('~');
                foreach (string s in strarr)
                    Console.WriteLine(s);
      

  4.   

    楼主的三个问题都是求源码哈!  internal static void PrintText30(string text)
            {
                do
                {
                    Console.WriteLine(text.Length > 30 ? text.Substring(0, 30) : text);
                    text = text.Length < 30 ? "": text.Substring(30);
                } while (text.Length > 0);
            }
            internal static void PrintText30(string text)
            {
                if (text.Length > 30)
                {
                    Console.WriteLine(text.Substring(0, 30));
                    TextDel(text.Substring(30));            }
                else
                    Console.WriteLine(text);
            }两个都可以用
     
      

  5.   


     internal static void PrintText30(string text)
            {
                do
                {
                    Console.WriteLine(text.Length > 30 ? text.Substring(0, 30) : text);
                    text = text.Length < 30 ? "": text.Substring(30);
                } while (text.Length > 0);
            }
            internal static void PrintText30(string text)
            {
                if (text.Length > 30)
                {
                    Console.WriteLine(text.Substring(0, 30));
                    PrintText30(text.Substring(30));            }
                else
                    Console.WriteLine(text);
            }
    楼上 弟二个方法内
    TextDel(text.Substring(30));
    这里错了
      

  6.   

    啊哦 晕! 有问题!!要<=   >=   要不长度刚好是30倍数的时候  会无限了!!
      

  7.   


     internal static void PrintText30(string text)
            {
                do
                {
                    Console.WriteLine(text.Length >= 30 ? text.Substring(0, 30) : text);
                    text = text.Length <= 30 ? "": text.Substring(30);
                } while (text.Length > 0);
            }
            internal static void PrintText30(string text)
            {
                if (text.Length >= 30)
                {
                    Console.WriteLine(text.Substring(0, 30));
                    PrintText30(text.Substring(30));            }
                else
                    Console.WriteLine(text);
            }
    不好意思 发太多了!
      

  8.   

    using System;
    public class Test
    {
        static void Main()
        {
            Print("第四纪咖啡馆看三大件敢死队公开赛大将军公司大家根据四大皆空第四纪敢死队");
            Console.ReadKey();
        }    static void Print(string str)
        { 
            int length=str.Length;
            while(length>30)
            {
                string strvalue = str.Substring(0,30);
                str = str.Remove(0, 30);
                length = str.Length;
                Console.WriteLine(strvalue);
            }
            Console.WriteLine(str);
        }
    }
      

  9.   

     static void Print(string str)
        {
            byte removelength = 30;
            int length=str.Length;
            while(length>30)
            {
                Console.WriteLine(str.Substring(0, removelength));
                str = str.Remove(0, removelength);
                length -= removelength;
            }
            Console.WriteLine(str);
        }
      

  10.   


    这样做先遍历替换一次,再遍历分割一次,影响效率,字符串越长,效率越低string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";
    MatchCollection mc = Regex.Matches(str, @".{30}|.{1,30}$");
    foreach (Match m in mc)
    {
        Console.WriteLine(m.Value);
    }
    Console.ReadLine();也可以这样,也是遍历一遍
    string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";
    List<string> result = new List<string>();
    result.Add(Regex.Replace(str, ".{30}", delegate(Match m){ result.Add(m.Value); return "";}));
    foreach(string s in result)
    {
        Console.WriteLine(s);
    }
    Console.ReadLine();不用正则也一样简单
    string str = "该公司的发第三件看法的上飞机千日分年年元角角发挥那可获得起而言和发卡行法高速钢复色光三个权热法机会热和人啊汉卡可让诶我去阿金卡你在那就擦";
    List<string> result = new List<string>();
    while (str.Length >= 30)
    {
        result.Add(str.Substring(0, 30));
        str = str.Substring(30);
    }
    result.Add(str);
    foreach (string s in result)
    {
        Console.WriteLine(s);
    }
    Console.ReadLine();