欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【ASP.NET两个截取字符串的实用方法技巧】,下面是详细的分享!
ASP.NET两个截取字符串的实用方法技巧
两个截取字符串的实用方法(超过一定长度自动换行)1///
2 /// 截取字符串,不限制字符串长度
3 ///
4 /// 待截取的字符串
5 /// 每行的长度,多于这个长度自动换行
6 ///
7 public string CutStr(string str,int len)
8 { string s="";
9
10 for(int i=0;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)+"
";
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 ///
37 /// 截取字符串并限制字符串长度,多于给定的长度+。。。
38 ///
39 /// 待截取的字符串
40 /// 每行的长度,多于这个长度自动换行
41 /// 输出字符串最大的长度
42 ///
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 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)+"
";
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 }
以上所分享的是关于ASP.NET两个截取字符串的实用方法技巧,下面是编辑为你推荐的有价值的用户互动:
相关问题:c#中怎样截取两特定字符之间的字符串
答:string stra = "abcdefghijk"; string strtempa = "c"; string strtempb = "j"; //我们要求c---g之间的字符串,也就是:defghi //求得strtempa 和 strtempb 出现的位置: int IndexofA = stra.IndexOf(strtempa); int IndexofB = stra.IndexOf(st... >>详细
相关问题:asp.net 字符串截取常用方法
答:string str="adcdef"; int indexStart = str.IndexOf("d"); int endIndex =str.IndexOf("e"); string toStr = str.SubString(indexStart,endIndex-indexStart); 搜索单词在字符串索引位置,然后再查找即可 >>详细
相关问题:c#中如何截取指定字符串以及旁边的几个字
答:先通过indexof获取百度的索引,如果有多个百度则通过递归方式从前往后找 找到第一个百度后,就可以把从0到第一个百度位置的内容取出来 然后继续判断是否有第二个百度,再把从上一个百度的索引和百度字符长度的和为起始位置,第二个百度为止位置... >>详细
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
