一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net字符串截取函数

时间:2010-08-24 编辑:简简单单 来源:一聚教程网

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(), "
");
            return tempstring;
        }

热门栏目