Asp.net string truncation Function

Source: Internet
Author: User

Function to intercept Chinese characters into one word and English into one word.

If you have any questions, please advise

 

/// <Summary>
/// Word truncation Function
/// </Summary>
/// <Param name = "splitstr"> string </param>
/// <Param name = "splitletter"> Number of truncated words </param>
/// <Returns> </returns>

Public String splitstring (string splitstr, int splitletter)
{
Int maxlength = splitletter * 2;
Int splitlpointer = splitletter;
Int splitlenght = splitstr. length;
String strafter = "";

If (splitlenght <splitletter)
Return splitstr;
If (splitletter <= 0)
Return string. empty;
If (system. Text. encoding. getencoding ("gb2312"). getbytes (splitstr). length> maxlength)
{Strafter = "...";}

Char [] splitchr = splitstr. tochararray ();
String splittemp = string. empty;

For (INT I = 0; I <splitlenght; I ++)
{
If (I = splitlpointer | I> = maxlength)
{Return splittemp + strafter ;}

Char splititem = splitchr [I];
Splittemp + = splititem. tostring ();
If (system. Text. encoding. getencoding ("gb2312"). getbytes (splititem. tostring (). Length = 1)
{Splitlpointer ++ ;}

Else if (system. Text. encoding. getencoding ("gb2312"). getbytes (splititem. tostring (). Length = 2)
{
If (splitlpointer = splitletter)
{
If (I + 2> splitlpointer)
{Return splittemp + strafter ;}
}
Else {
If (I + 2> = splitlpointer)
{Return splittemp + strafter ;}
}

}
}
Return splitstr;

}

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.