惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Schneier on Security
Schneier on Security
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
阮一峰的网络日志
阮一峰的网络日志
博客园_首页
The GitHub Blog
The GitHub Blog
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
罗磊的独立博客
H
Help Net Security
博客园 - 聂微东
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence
有赞技术团队
有赞技术团队
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
G
GRAHAM CLULEY
S
Schneier on Security
A
About on SuperTechFans
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
T
The Exploit Database - CXSecurity.com
Y
Y Combinator Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Register - Security
The Register - Security
P
Proofpoint News Feed
Jina AI
Jina AI
Latest news
Latest news
T
Threat Research - Cisco Blogs
V
Visual Studio Blog
P
Privacy International News Feed
H
Hacker News: Front Page
Application and Cybersecurity Blog
Application and Cybersecurity Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Threatpost
T
Tor Project blog
SecWiki News
SecWiki News
Google Online Security Blog
Google Online Security Blog
O
OpenAI News
T
Tenable Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - 无极.net

.NET正则表达式使用高级技巧之工作特点 .NET正则表达式使用高级技巧之反向引用 - 无极.net - 博客园 .NET正则表达式使用高级技巧之组的概念 - 无极.net - 博客园 .NET正则表达式使用高级技巧之替换类 C#中利用正则表达式实现字符串搜索 c# 添加图片水印,可以指定水印位置+生成缩略图 - 无极.net - 博客园 FLASH图片新闻代码 - 无极.net - 博客园 正则表达式30分钟入门教程 v2.1 两个固定宽度的DIV,不换行 - 无极.net - 博客园 UrlReWriter 使用经验小结 如何在JS里取得两个数相除的整数和余数,那么如何判断一个数是整数呢? 向上滚动 flash从asp中调用变量 菜单 ASP.NET AJAX(开发代号Atlas)重要参考资源大收集 索引[]在字符串中的用法 div 相对于浮动层定位,不占位 储存过程里设置了OUTPUT,取值 SQL 声明变量 declare
字符串长度,同时考虑二个英文=一个中文 - 无极.net - 博客园
无极.net · 2007-07-02 · via 博客园 - 无极.net

/// <summary>
    /// 截获定长的字符串
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <param name="length">需要截获的长度</param>
    /// <returns>截获后的字符串</returns>
    static public string FixLenth ( string source, int length)
    {
      return FixLenth ( source, length, "...");
    }
    /// <summary>
    /// 截获定长的字符串
    /// </summary>
    /// <param name="source">源字符串</param>
    /// <param name="length">需要截获的长度</param>
    /// <param name="postfix">如果字符串被截短,需要添加什么样的后缀</param>
    /// <returns>截获后的字符串</returns>
    static public string FixLenth ( string source, int length, string postfix)
    {
      if ( source == null )
        throw new ArgumentNullException( "source" );
      if ( postfix == null )
        postfix = "...";

      if ( length < postfix.Length )
        throw new ArgumentOutOfRangeException( "length" );

      int postfixLength = System.Text.Encoding.GetEncoding("GB2312").GetByteCount( postfix);
      int srcLength = System.Text.Encoding.GetEncoding("GB2312").GetByteCount( source);

      if ( srcLength > length)
      {
        for ( int i = source.Length; i>0; i--)
        {
          srcLength = System.Text.Encoding.GetEncoding("GB2312").GetByteCount( source.Substring(0,i) );

          if ( srcLength <= length - postfixLength)
            return source.Substring(0,i) + postfix;
        }
        return "";
      }
      else
        return source;
    }

--------------------------------------------------------

能给个思路吗?
我想知道是怎么做的

--------------------------------------------------------

GB2312编码会将一个中文字编码成两个字节,而英文字母只会编码成一个字节,那么根据编码后的字节数就能知道是否满足截断的要求了。那么,弄个循环不断的缩短截断长度直到满足要求为止就行了。

--------------------------------------------------------

就像这样
int postfixLength = System.Text.Encoding.GetEncoding("GB2312").GetByteCount( postfix);
      int srcLength = System.Text.Encoding.GetEncoding("GB2312").GetByteCount( source);