前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微博内容长度计算函数(C#)

微博内容长度计算函数(C#)

作者头像
张善友
发布2018-01-22 12:09:02
7500
发布2018-01-22 12:09:02
举报
文章被收录于专栏:张善友的专栏张善友的专栏

微博内容长度的计算方法(不一定完全无误,仅提供一个思路):

代码语言:js
复制
       public static int GetWeiboContentLength(string weiboContent) 
       {    
           var max = 140;    
           var surl = 11;    
           var urlCount = 0; 
           RegexOptions ops = RegexOptions.Compiled;   
           Regex regex = new Regex(@"http://[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+([-A-Z0-9a-z_\$\.\+\!\*\(\)\/,:;@&=\?\~\#\%]*)", ops);    
           var content = weiboContent;    
           if (regex.IsMatch(weiboContent))    
           {    
               var matchs = regex.Matches(weiboContent);    
               for (int i = 0; i < matchs.Count; i++)    
               {    
                   var count = matchs[i].Groups[0].Length;    
                   urlCount += count <= max ? surl : (count - max + surl);    
                   content = content.Replace(matchs[i].Value, "");    
               }    
           }    
           var result = urlCount + content.Length;    
           return result;    
       }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-07-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档