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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 无极.net

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