Asp. NET tip: Two practical ways to intercept strings

Source: Internet
Author: User
Asp.net| Tips | string

  Two practical ways to intercept strings (wrap more than a certain length)

1/**////<summary>
2///intercept string, no Limit string length
3///</summary>
4///<param name= "str" > string to intercept </param>
5///<param name= "len" > length of each line, more than this length of automatic line wrap </param>
6///<returns></returns>
7 public string Cutstr (string Str,int len)
8 {string s= "";
9
for (int i=0;i<str. Length; i++)
11 {
int r= i% Len;
int last = (str. Length/len) *len;
if (i!=0 && i<=last)
15 {
16
if (r==0)
18 {
S+=str. Substring (I-len,len) + "<br>";
20}
21st
22}
Or else if (i>last)
24 {
S+=str. Substring (i-1);
-Break;
27}
28
29}
30
return s;
32
33}
34
35
/**////<summary>
37///Intercept the string and limit the length of the string, more than the given length + ...
///</summary>
///<param name= "str" > the string to be intercepted </param>
///<param name= "len" > length of each line, more than this length automatic line wrap </param>
///<param name= "Max" > Output string Maximum length </param>
///<returns></returns>
The public string cutstr (string str,int len,int max)
44 {
s= string "";
sheng= string "";
The IF (str). Length >max)
48 {
Str=str. Substring (0,max);
Sheng= "";
51}
The for (int i=0;i<str. Length; i++)
53 {
The int r= i% len;
int last = (str. Length/len) *len;
if (i!=0 && i<=last)
57 {
58
The IF (r==0)
60 {
S+=str. Substring (I-len,len) + "<br>";
62}
63
64}
An else if (i>last)
66 {
S+=str. Substring (i-1);
a break;
69}
70
71}
72
S+sheng return;
74
75}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.