首页 > 编程 > .NET > 正文

ASP.NET技巧:两个截取字符串的实用方法

2024-07-10 13:09:31
字体:
来源:转载
供稿:网友

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

 1/**//// <summary>
 2        /// 截取字符串,不限制字符串长度
 3        /// </summary>
 4        /// <param name="str">待截取的字符串</param>
 5        /// <param name="len">每行的长度,多于这个长度自动换行</param>
 6        /// <returns></returns>
 7        public string cutstr(string str,int len)
 8        {    string s="";
 9           
10            for(int i=0;i<str.length ;i++)
11            {
12                int r= i% len;
13                int last =(str.length/len)*len;
14                if (i!=0 && i<=last)
15                {
16                   
17                    if( r==0)
18                    {
19                        s+=str.substring(i-len,len)+"<br>";
20                    }
21                       
22                }
23                else if (i>last)
24                {
25                    s+=str.substring(i-1) ;
26                    break;
27                }
28               
29            }
30           
31            return s;
32           
33        }
34
35
36        /**//// <summary>
37        /// 截取字符串并限制字符串长度,多于给定的长度+。。。
38        /// </summary>
39        /// <param name="str">待截取的字符串</param>
40        /// <param name="len">每行的长度,多于这个长度自动换行</param>
41        /// <param name="max">输出字符串最大的长度</param>
42        /// <returns></returns>
43        public string cutstr(string str,int len,int max)
44        {
45            string s="";
46            string sheng="";
47            if (str.length >max)
48            {
49                str=str.substring(0,max) ;
50                sheng="";
51            }
52            for(int i=0;i<str.length ;i++)
53            {
54                int r= i% len;
55                int last =(str.length/len)*len;
56                if (i!=0 && i<=last)
57                {
58                   
59                    if( r==0)
60                    {
61                        s+=str.substring(i-len,len)+"<br>";
62                    }
63                       
64                }
65                else if (i>last)
66                {
67                    s+=str.substring(i-1) ;
68                    break;
69                }
70               
71            }
72           
73            return s+sheng;
74           
75        }

注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表