两个截取字符串的实用方法(超过一定长度自动换行)

 1 ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
 2InBlock.gif        /// 截取字符串,不限制字符串长度
 3InBlock.gif        /// </summary>
 4InBlock.gif        /// <param name="str">待截取的字符串</param>
 5InBlock.gif        /// <param name="len">每行的长度,多于这个长度自动换行</param>
 6ExpandedBlockEnd.gif        /// <returns></returns>

 7 None.gif          public   string  CutStr( string  str, int  len)
 8 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {    string s="";
 9InBlock.gif            
10InBlock.gif            for(int i=0;i<str.Length ;i++)
11ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
12InBlock.gif                int r= i% len;
13InBlock.gif                int last =(str.Length/len)*len;
14InBlock.gif                if (i!=0 && i<=last)
15ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
16InBlock.gif                    
17InBlock.gif                    if( r==0)
18ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
19InBlock.gif                        s+=str.Substring(i-len,len)+"<br>";
20ExpandedSubBlockEnd.gif                    }

21InBlock.gif                        
22ExpandedSubBlockEnd.gif                }

23InBlock.gif                else if (i>last)
24ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
25InBlock.gif                    s+=str.Substring(i-1) ;
26InBlock.gif                    break;
27ExpandedSubBlockEnd.gif                }

28InBlock.gif                
29ExpandedSubBlockEnd.gif            }

30InBlock.gif            
31InBlock.gif            return s;
32InBlock.gif            
33ExpandedBlockEnd.gif        }

34 None.gif
35 None.gif
36 ExpandedBlockStart.gifContractedBlock.gif         /**/ /// <summary>
37InBlock.gif        /// 截取字符串并限制字符串长度,多于给定的长度+。。。
38InBlock.gif        /// </summary>
39InBlock.gif        /// <param name="str">待截取的字符串</param>
40InBlock.gif        /// <param name="len">每行的长度,多于这个长度自动换行</param>
41InBlock.gif        /// <param name="max">输出字符串最大的长度</param>
42ExpandedBlockEnd.gif        /// <returns></returns>

43 None.gif          public   string  CutStr( string  str, int  len, int  max)
44 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
45InBlock.gif            string s="";
46InBlock.gif            string sheng="";
47InBlock.gif            if (str.Length >max)
48ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
49InBlock.gif                str=str.Substring(0,max) ;
50InBlock.gif                sheng="dot.gif";
51ExpandedSubBlockEnd.gif            }

52InBlock.gif            for(int i=0;i<str.Length ;i++)
53ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
54InBlock.gif                int r= i% len;
55InBlock.gif                int last =(str.Length/len)*len;
56InBlock.gif                if (i!=0 && i<=last)
57ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
58InBlock.gif                    
59InBlock.gif                    if( r==0)
60ExpandedSubBlockStart.gifContractedSubBlock.gif                    dot.gif{
61InBlock.gif                        s+=str.Substring(i-len,len)+"<br>";
62ExpandedSubBlockEnd.gif                    }

63InBlock.gif                        
64ExpandedSubBlockEnd.gif                }

65InBlock.gif                else if (i>last)
66ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
67InBlock.gif                    s+=str.Substring(i-1) ;
68InBlock.gif                    break;
69ExpandedSubBlockEnd.gif                }

70InBlock.gif                
71ExpandedSubBlockEnd.gif            }

72InBlock.gif            
73InBlock.gif            return s+sheng;
74InBlock.gif            
75ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/goody9807/archive/2006/09/26/515297.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值