asp教程.net字符串截取函数
本教程先是告诉你用最简单的方法取对字符串进行截取,再后面写了一个实用的程序来对数据进行字符串截取哦。
*/
//c#
string mystring = "abc";
bool test1 = mystring.substring(2, 1).equals("c"); // this is true.//vb
substring
microsoft.visualbasic.left(string, length)
//一个简单智能一点的字符串截取函数protected string getstr(string str)
{if (str.length > 15)
{
str = str.substring(0, 15) + "...";
}
return str;
}
//截取实例public static string formatstr(string str, int len)
{
asciiencoding ascii = new asciiencoding();
int templen = 0;
string temps教程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;
}
//如果截过则加上半个省略号
byte[] mybyte = system.text.encoding.default.getbytes(str);
if (mybyte.length > len)
tempstring += "......";
tempstring = tempstring.replace(" ", " ");
tempstring = tempstring.replace("<", "<");
tempstring = tempstring.replace(">", ">");
tempstring = tempstring.replace('n'.tostring(), "<br>");
return tempstring;
}