Asp.net string truncation method

Source: Internet
Author: User

Public partial class substring: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
This. lblsub. Text = getfirststring ("Chinese", 7 );
This. lblsub2.text = getfirststring ("afafaf hello", 7 );
}

Public String getstr (string strinput, int intlen)
{
Strinput = strinput. Trim ();
Byte [] mybyte = system. Text. encoding. Default. getbytes (strinput );
Response. Write ("getstr function is:" + mybyte. length. tostring ());
If (mybyte. length> intlen)
{
// Screenshot operation
String resultstr = "";
For (INT I = 0; I <strinput. length; I ++)
{
Byte [] tempbyte = system. Text. encoding. Default. getbytes (resultstr );
If (tempbyte. Length <intlen)
{
Resultstr + = strinput. substring (I, 1 );
}
Else
{
Break;
}
}
Return resultstr + "...";
}
Else
{
Return strinput;
}
}

Public String cutstring (string strinput, int intlen)
{
Strinput = strinput. Trim ();
Byte [] mybyte = system. Text. encoding. Default. getbytes (strinput );
Response. Write ("cutstring function is:" + mybyte. length. tostring ());
If (mybyte. length> intlen)
{
// Screenshot operation
String resultstr = "";
For (INT I = 0; I <strinput. length; I ++)
{
Byte [] tempbyte = system. Text. encoding. Default. getbytes (resultstr );
If (tempbyte. Length <intlen)
{

Resultstr + = strinput. substring (I, 1 );
}
Else
{
Break;
}
}
Return resultstr + "...";
}
Else
{
Return strinput;
}
}

Public static string fix (string S, int Len)
{
String result = ""; // The final returned result.
Int bytelen = system. Text. encoding. Default. getbytecount (s); // the length of a single byte
Int charlen = S. length; // String Length when the characters are treated equally
Int bytecount = 0; // record the read progress {Chinese is calculated in two units}
Int Pos = 0; // record truncation position {Chinese is calculated in two units}
If (bytelen> Len)
{
For (INT I = 0; I <charlen; I ++)
{
If (convert. toint32 (S. tochararray () [I])> 255) // In case of Chinese Character Count plus 2
Bytecount + = 2;
Else // Add 1 to the English character
Bytecount + = 1;
If (bytecount> = Len) // when the specified length is reached, record the pointer position and stop
{
Pos = I;
Break;
}
}
Result = S. substring (0, POS) + "...";
}
Else
Result = s;

Return result;
}

# Region functions for truncating strings. partitions are in Chinese and English.
/// <Summary>
/// Functions for truncating strings
/// </Summary>
/// <Param name = "mtext"> string to be processed </param>
/// <Param name = "bytecount"> length </param>
/// <Returns> processed strings </returns>
Public String left (string mtext, int bytecount)
{
If (bytecount <1) return mtext;

If (system. Text. encoding. Default. getbytecount (mtext) <= bytecount)
{
Return mtext;
}
Else
{
Byte [] txtbytes = system. Text. encoding. Default. getbytes (mtext );
Byte [] newbytes = new byte [bytecount-4];

For (INT I = 0; I <bytecount-4; I ++)
{
Newbytes [I] = txtbytes [I];
}

Return System. Text. encoding. Default. getstring (newbytes) + "...";
}
}
# Endregion

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 ();
}
}

 

. NET Technology Discussion Group 4151320

Welcome to join us !!!

 

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.