1.判断空格来确定单词是否完整
2.累计字符数是否是20的倍数.
3.":"数量i<2
4.":"数量i==2
5.满足1,2,3条件插入":"; 并将":"数量i=i+1 
6.满足1,2,3,4条件换行; 并将":"数量i=0

解决方案 »

  1.   

    定义一个string类型的变量并赋值,然后通过调用string变量中的方法可以非常方便的获取里面的字符,详细信息可以参考MSDN。
      

  2.   

    上面写的不完整.
    1.累计字符数n=n+1,n>20
    2.判断空格来确定单词是否完整
    3.":"数量i<2
    4.":"数量i==2
    5.1,2,3条件插入":"; 并将":"数量i=i+1,n=0,
    6.满足1,2,3,4条件换行; 并将":"数量i=0,n=0
    执行此循环.
      

  3.   

    public string zhuanhuan(string str,int i,int j)
    {    string result;
        int k=0;
         string[] s = str.Split(new Char[]{' '});
         for(int l =2;l<s.Lenth ; l +=3) 
        {     
              k++;
               if(s[l].Length>j)
               {
                  s[l] = s[l].SubString(0,j);
                }
             result =result + "this is "+s[l];
             if(k==i)
            {
                    result +="\n";
                 k =0;
            }else
               result+= ":";
              
             
        }    
         return result ;}
      

  4.   

    每组20个字符如果是包含“this is ”部分在内的话:
    改成:
    public string zhuanhuan(string str,int i,int j)
    {    string result;
        int k=0;
           int qi = "this is".Length;
         string[] s = str.Split(new Char[]{' '});
         for(int l =2;l<s.Length ; l +=3) 
        {     
              k++;
               if(s[l].Length>j-qi)
               {
                  s[l] = s[l].SubString(0,j-qi);
                }
             result =result + "this is "+s[l];
             if(k==i)
            {
                    result +="\n";
                 k =0;
            }else
               result+= ":";
              
             
        }    
         return result ;}
      

  5.   

    string[] Apart(string s,int maxSize,int groups,string seperator)
    {
    if (s.Length < maxSize) return new string[]{s};
    ArrayList al = new ArrayList();
    int i = 0,j = 0,k = 0;
    string sTemp;
    while (i < s.Length)
    {
    j = s.LastIndexOf(" ",i,20);
    if (j > 0)
    {
    sTemp += s.Substring(i,j - i) + seperator;
    ++k;
    if (k == 3)
    {
    k = 0;
    al.Add(sTemp.Substring(0,sTemp.Length - seperator.Length));
    sTemp = "";
    }
    }
    else
    {
    al.Add(s.Substring(i));
    break;
    }
    }
    return al.ToArray(typeof(string)); }
      

  6.   

    private string Trans(string vSource,int countPerRow,int countPerGroup)
    {
    string s,result;
    int i;
    result="";
    i=0;
    s=vSource;
    while(s.Length >countPerGroup)
    {
    i++;
    result=result+ s.Substring(1,countPerGroup);
    if ((i%3)==0)
    result =result + "\n";
    else
    result=result+":";
    s=s.Substring(countPerGroup,s.Length);
    }
    if (s.Length >0)
    result =result+s;
    return result;
    }
      

  7.   

    更正:
    if ((i%3)==0)改为if ((i%countPerRow)==0)
      

  8.   

    楼上的应该使用string[]类型吧
      

  9.   

    jialiang:s=s.Substring(countPerGroup,s.Length);这句报错
      

  10.   

    using System.Text.RegularExpressions;/******************/private void GetReplacedText( string content )
    {
       return Regex.Replace( content , @"(?<G1>this\s+is\s+.*?(?=this\s+is\s+))(?<G>this\s+is\s+.*?(?=this\s+is\s+))(?<G3>this\s+is\s+.*?(?=this\s+is\s+))" , 
                                            @"$1:$2:$3" + "\r\n" , RegexOptions.Singleline | RegexOptions.IgnoreCase );
    }
      

  11.   

    using System.Text.RegularExpressions;/******************/private string GetReplacedText( string content )
    {
    string replacedText = Regex.Replace( content , @"(?<G1>this\s+is\s+.*?(?=this\s+is\s+))(?<G2>this\s+is\s+.*?(?=this\s+is\s+))(?<G3>this\s+is\s+.*?(?=this\s+is\s+))|(?<G1>this\s+is\s+.*?(?=this\s+is\s+))(?<G2>this\s+is\s+.*?$)" , 
    "${G1}:${G2}:${G3}" + "\r\n", RegexOptions.Singleline | RegexOptions.IgnoreCase ).TrimEnd();
       
    if (replacedText.EndsWith(":"))
    {
    replacedText =  replacedText.Substring( 0 , replacedText.Length - 2 ); 
    }
    return replacedText;
    }
      

  12.   

    s=s.Substring(countPerGroup,s.Length);这句报错
    应该改为s=s.Substring(countPerGroup,s.Length-countPerGroup);
      

  13.   

    jialiang兄您截取的是对的,但没有考虑到:
    每组的最后一个单词是完整的,即可以每组长度可以不到规定的每组字符数。