参考:http://www.cnblogs.com/lollipop/archive/2012/09/16/2687158.html,谢谢,思路不错:
classProgram
{
static void Main (string[] args)
{
string str ="hslkjdlkajfiejlksjlifjelkjflksjfd;laijeflkjlfjaiojelkfjslkjdfijefjlkdfjlisjek";
//string str = "晒单啦,晒单啦,黑色星期五赚到啦~~ @周鸿祎 @360商城 秒杀成功,【黑五扫货节】360安全路由P1(土豪金)正在过来的路上啦。。。期待。。。1元秒杀成功,5块钱的运费,6块钱绝对赚到,期待快递小哥能够快点~~~订单号漏出来了哦,求优惠券@360商城 ";
var strAfter = TextLength(str,20);
Console.WriteLine(str);
Console.WriteLine("\n"+ strAfter);
Console.WriteLine("Hello World!");
Console.ReadKey();
}
static string TextLength(string s,int l)
{
char[] s1 = s.ToCharArray();
int now1 =0;
StringBuilder sb =new StringBuilder();
for(inti =0; i < s1.Length; i++)
{
if((int)s1[i] >127)
{
now1 +=2;
if(now1 > l)
{
sb.Append("...");
return sb.ToString();
}
else{
sb.Append(s1[i]);
}
if(now1 == l)
{
sb.Append("...");
return sb.ToString();
}
}
else{
now1++;
if(now1 >= l)
{
sb.Append(s1[i]);
sb.Append("...");
return sb.ToString();
}
else{
sb.Append(s1[i]);
}
}
}
return sb.ToString();
}
}
网友评论