[Reprint] C # string truncation function written

Source: Internet
Author: User

Today, when I wrote the same method, I searched for something on the internet and found several well-written methods. I took a note temporarily. Please refer to it. I only want to learn for reference. I don't have time to sort it out. I just want to put it here. Let's take a look at it later.

String truncation Function

Public String cutstr (string sinstring, int icutlength ){
If (sinstring = NULL | sinstring. Length = 0 | icutlength <= 0) Return "";
Int icount = system. Text. encoding. getencoding ("shift_jis"). getbytecount (sinstring );
If (icount> icutlength ){
Int ilength = 0;
For (INT I = 0; I <sinstring. length; I ++ ){
Int icharlength = system. Text. encoding. getencoding ("shift_jis"). getbytecount (New char [] {sinstring [I]});
Ilength + = icharlength;
If (ilength = icutlength ){
Sinstring = sinstring. substring (0, I + 1 );
Break;
} Else if (ilength> icutlength ){
Sinstring = sinstring. substring (0, I );
Break;
}
}
}
Return sinstring;
}

How to intercept strings in ASP. NET or C #

Protected string cutstring (string STR, int length)
{
String newstring = "";
If (STR! = "")
{
If (Str. length> length)
{
Newstring = Str. substring (0, length) + "...";
}
Else
{
Newstring = STR;
}
}
Return newstring;
}

Then, when binding:
<% # Cutstring (databinder. eval (container. dataitem, "newtitle"). tostring (), 5) %>

 

In the Asp.net (C #) datelist DataGrid, extract the string and add "..." and put the mouse on it to display all the characters.

Front-end
<Asp: templatecolumn headertext = "binding URL">
<Headerstyle horizontalalign = "center" width = "100px"> <Itemstyle horizontalalign = "center"> </itemstyle>
<Itemtemplate>
<Font face = "">
<Asp: hyperlink id = "hyperlink1" runat = "server" target = _ blank text = '<% # partsubstring (databinder. eval (container. dataitem, "url "). tostring () %> 'navigateurl = '<% # databinder. eval (container. dataitem, "url") %> 'tooltip = '<% # databinder. eval (container. dataitem, "url") %> '>
</ASP: hyperlink> </font>
</Itemtemplate>
<Footerstyle horizontalalign = "center"> </footerstyle>
</ASP: templatecolumn>
Backend. CS
Protected string partsubstring (string S)
{
If (S. length> 15)
{
Return S. substring (0, 15) + "...";
}
Return S;
}

Use C # to intercept a string of the specified length.

One thing we often do isArticleIn the system, extract a certain length of article title. If the length exceeds the specified length, add "...".

For example, two strings:
String str1 = "What are Chinese? Abc ~ ";
String str2 = "1 Chinese 23456abc Ah ~ ";

Output after intercepting:

Str1 = "Chinese want ...";
Str2 = "1 Chinese 2 ...";

That is to say, the length of a string that contains both Chinese and English characters must be the same after the truncation, that is, the length of 8 bytes (excluding three points ), in addition, Chinese characters cannot be truncated from the middle. So I wrote a method:

Public static string getstr (string S, int L)
{< br> string temp = s;
If (RegEx. replace (temp, "[\ u4e00-\ u9fa5]", "ZZ", regexoptions. ignorecase ). length <= L)
{< br> return temp;
}< br> for (INT I = temp. length; I> = 0; I --)
{< br> temp = temp. substring (0, I);
If (RegEx. replace (temp, "[\ u4e00-\ u9fa5]", "ZZ", regexoptions. ignorecase ). length <= L-3)
{< br> return temp + ";
}< BR >}< br> return "";
}< br> call:
string content = "Chinese ABC highlight ah";
content = getstr (content, 13);

C # method for intercepting a string of a specified length in Chinese and English ()
String S = "Iam gun ";
Int Len = S. length; // will output as 6
Byte [] Sarr = system. Text. encoding. Default. getbytes (s );
Len = Sarr. length; // will output as 3 + 3*2 = 9 public static string getfirststring (string stringtosub, int length)
{
RegEx = new RegEx ("[\ u4e00-\ u9fa5] +", regexoptions. Compiled );
Char [] stringchar = stringtosub. tochararray ();
Stringbuilder sb = new stringbuilder ();
Int nlength = 0;
Bool iscut = false;
For (INT I = 0; I <stringchar. length; I ++)
{
If (RegEx. ismatch (stringchar [I]). tostring ()))
{
SB. append (stringchar [I]);
Nlength + = 2;
}
Else
{
SB. append (stringchar [I]);
Nlength = nlength + 1;
} If (nlength> length)
{
Iscut = true;
Break;
}
}
If (iscut)
Return sb. tostring () + "..";
Else
Return sb. tostring ();
}

C # How to intercept a specified length of Chinese and English strings (modify)

Public static string getfirststring (string stringtosub, int length)
{
RegEx = new RegEx ("[\ u4e00-\ u9fa5] +", regexoptions. Compiled );
Char [] stringchar = stringtosub. tochararray ();
Stringbuilder sb = new stringbuilder ();
Int nlength = 0;
For (INT I = 0; I <stringchar. length; I ++)
{
If (RegEx. ismatch (stringchar [I]). tostring ()))
{
Nlength + = 2;
}
Else
{
Nlength = nlength + 1;
}

If (nlength <= length)
{
SB. append (stringchar [I]);
}
Else
{
Break;
}
}
If (sb. tostring ()! = Stringtosub)
{
SB. append ("...");
}
Return sb. tostring ();
} 

 

**************************************** **************

1/** // <summary>
2 // intercept a string without specifying the length of the string
3 /// </Summary>
4 /// <Param name = "str"> string to be truncated </param>
5 /// <Param name = "len"> length of each line, more than the length of this line automatically wrap </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 // capture the string and limit the length of the string, more than the given length +...
38 /// </Summary>
39 // <Param name = "str"> string to be intercepted </param>
40 /// <Param name = "len"> length of each line, more than the length of this line automatically wrap </param>
41 // <Param name = "Max"> Maximum length of the output string </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}

**************************************** ******************

C # using system. Text

Public static string getfirststring (string stringtosub, int length)
{
RegEx = new RegEx ("[\ u4e00-\ u9fa5] +", regexoptions. Compiled );
Char [] stringchar = stringtosub. tochararray ();
Stringbuilder sb = new stringbuilder ();
Int nlength = 0;
Bool iscut = false;
For (INT I = 0; I <stringchar. length; I ++)
{
If (RegEx. ismatch (stringchar [I]). tostring ()))
{
SB. append (stringchar [I]);
Nlength + = 2;
}
Else
{
SB. append (stringchar [I]);
Nlength = nlength + 1;
}

If (nlength> length)
{
Iscut = true;
Break;
}
}
If (iscut)
Return sb. tostring () + "..";
Else
Return sb. tostring ();
}

I found the method from the Internet and added it. If the length is exceeded, add the ".." character later.

This is the most useful way to display news titles when you create a website. To maintain the page pattern, you need to limit the title length, which requires two-character Chinese characters.

Table line feedCode:

Style = "table-layput: fixed; Word-wrap: Break-wrod; Word-break: Break-all ;"


Related Article

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.