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

推荐订阅源

Y
Y Combinator Blog
美团技术团队
H
Hacker News: Front Page
Spread Privacy
Spread Privacy
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tenable Blog
Simon Willison's Weblog
Simon Willison's Weblog
T
The Exploit Database - CXSecurity.com
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
C
CXSECURITY Database RSS Feed - CXSecurity.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
F
Fortinet All Blogs
量子位
GbyAI
GbyAI
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
Forbes - Security
Forbes - Security
Help Net Security
Help Net Security
I
InfoQ
有赞技术团队
有赞技术团队
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
S
Secure Thoughts
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Webroot Blog
Webroot Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
C
Check Point Blog
T
Troy Hunt's Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Latest news
Latest news
P
Proofpoint News Feed
Jina AI
Jina AI
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
雷峰网
雷峰网
博客园 - Franky
L
LangChain Blog
罗磊的独立博客
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
D
Docker
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC

博客园 - 无极.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);