Asp.net string truncation Function

Source: Internet
Author: User

Asp.net string truncation function this tutorial first tells you to extract the string in the simplest way, and then write a utility program to intercept the data string.

Asp tutorial. net string truncation Function
In this tutorial, we will first show you the simplest way to take the string and then write a practical program to intercept the data string.
*/

// C #
String mystring = "abc ";
Bool test1 = mystring. substring (2, 1). equals ("c"); // this is true.

// Vb

Substring
Microsoft. visualbasic. left (string, length)
// A simple and intelligent string truncation Function

Protected string getstr (string str)
{

If (str. length> 15)
{
Str = str. substring (0, 15) + "...";
}
Return str;
}
// Intercept an instance

Public static string formatstr (string str, int len)
{
Asciiencoding ascii = new asciiencoding ();
Int templen = 0;
String temps tutorial tring = "";
Byte [] s = ascii. getbytes (str );
For (int I = 0; I <s. length; I ++)
{
If (int) s [I] = 63)
{Templen + = 2 ;}
Else
{Templen + = 1 ;}
Try
{Tempstring + = str. substring (I, 1 );}
Catch
{Break ;}
If (templen> len) break;
}
// If yes, add half ellipsis
Byte [] mybyte = system. text. encoding. default. getbytes (str );
If (mybyte. length> len)
Tempstring + = "......";
Tempstring = tempstring. replace ("& nbsp ;","");
Tempstring = tempstring. replace ("& lt;", "<");
Tempstring = tempstring. replace ("& gt;", "> ");
Tempstring = tempstring. replace ('n'. tostring (), "<br> ");
Return tempstring;
}

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.