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

推荐订阅源

Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 三生石上(FineUI控件)
云风的 BLOG
云风的 BLOG
C
Check Point Blog
Engineering at Meta
Engineering at Meta
Y
Y Combinator Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Forbes - Security
Forbes - Security
IT之家
IT之家
L
LINUX DO - 最新话题
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
雷峰网
雷峰网
N
News | PayPal Newsroom
The Last Watchdog
The Last Watchdog
V
Visual Studio Blog
月光博客
月光博客
Microsoft Azure Blog
Microsoft Azure Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
TaoSecurity Blog
TaoSecurity Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Schneier on Security
Schneier on Security
P
Privacy International News Feed
G
Google Developers Blog
博客园 - 聂微东
博客园 - 叶小钗
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Simon Willison's Weblog
Simon Willison's Weblog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
H
Hacker News: Front Page
Martin Fowler
Martin Fowler
L
Lohrmann on Cybersecurity
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
T
The Exploit Database - CXSecurity.com
S
Security @ Cisco Blogs
博客园_首页
AWS News Blog
AWS News Blog
P
Proofpoint News Feed
人人都是产品经理
人人都是产品经理
Help Net Security
Help Net Security
Google DeepMind News
Google DeepMind News
T
Threatpost

博客园 - angushine

获得本机IP地址 设置eclipse文件默认的编码方式 Eclipse Helios的PermGen space错误的解决办法 取指定长度的字符串(双字节算2) - angushine - 博客园 Silverlight项目无法启动调试 增强 VSS 的文件共享安全性 兼容各个版本浏览器的设置最小宽度 动态分配一维数组 浏览器区别 封装log4net C#获得调用方法的名称和类名 jQuery全选/全消CheckBox以及JS回调一例 MySQL绿色安装方法1 WebBrowser中调用加载页面的Javascript方法 定时更新内存和虚拟内存 多线程中更新组件 Request、Request.QueryString、Request.Form与Request.Params 反射调用静态方法 开始→运行→输入的命令集锦(转载)
截取指定长度字符串
angushine · 2010-01-07 · via 博客园 - angushine

[\u0391-\uFFE5]匹配双字节字符(汉字+符号)

[\u4e00-\u9fa5]注意只匹配汉字,不匹配双字节字符

代码


/// <summary>
/// 截取指定长度的字符串(Unicode占两位)
/// </summary>
/// <param name="source">指定字符串</param>
/// <param name="length">指定长度</param>
/// <returns>返回指定字符串长度的</returns>
public static string Sub(string source, int length)
{
    
string temp = source;
    
if (Regex.Replace(temp, "[\u0391-\uffe5]""  ", RegexOptions.IgnoreCase).Length <= length)
    {
        
return temp;
    }
    
for (int i = temp.Length; i >= 0; i--)
    {
        temp 
= temp.Substring(0, i);
        
if (Regex.Replace(temp, "[\u0391-\uffe5]""  ", RegexOptions.IgnoreCase).Length <= length)
        {
            
return temp + "";
        }
    }
    
return "";
}

 上面这种执行效率比下面这种方法要高,耗时要短:

代码

/// <summary>
/// 截取指定长度的字符串(Unicode占两位)
/// </summary>
/// <param name="source">指定字符串</param>
/// <param name="length">指定长度</param>
/// <returns>返回指定字符串长度的</returns>
public static string SLeft(string source, int length)
{
    Regex regex 
= new Regex("[\u0391-\uffe5]+", RegexOptions.Compiled);
    
char[] stringChar = source.ToCharArray();
    
string sb = "";
    
int nLength = 0;
    
for (int i = 0; i < stringChar.Length; i++)
    {
        
if (nLength >= length)
        {
            
break;
        }
        
if (regex.IsMatch((stringChar[i]).ToString()))
        {
            sb 
+= stringChar[i];
            nLength 
+= 2;
        }
        
else
        {
            sb 
+= stringChar[i];
            nLength 
= nLength + 1;
        }
    }
    
return sb.ToString();
}

 下面这种方法耗时最短,效率最高:

代码

public static string GetStr(string source, int length)
{
    
int count = 0;
    
for (int i = 0; i < source.Length; i++)
    {
        
if (((int)source[i]).ToString("X2").Length == 4)
            count 
+= 2;
        
else
            count 
+= 1;
    }
    
if (count > length)
    {
        StringBuilder sb 
= new StringBuilder();
        count 
= 0;
        
for (int i = 0; i < source.Length; i++)
        {
            
char temp = source[i];
            
if (((int)temp).ToString("X2").Length == 4)
            {
                count 
+= 2;
            }
            
else
            {
                count 
+= 1;
            }
            
if (count < length)
            {
                sb.Append(temp);
            }
            
else if (count == length)
            {
                sb.Append(temp);
                
break;
            }
            
else if (count > length)
            {
                sb.Append(
"");
                
break;
            }
        }
        
return sb.ToString();
    }
    
else
    {
        
return source;
    }
}

测试代码如下:

代码

string source = "《中国》按时打发按时打发撒地方啊飒飒asdfasdfasd的法师打发似的<发(生的发生)地方按时打发是asdfasdfas";
int length = 10;

DateTime dt 

= DateTime.Now;
for (int i = 0; i < 1000; i++)
{
    
string temp = StrFun.SLeft(source, length);
}
TimeSpan ts 
= DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);

dt 

= DateTime.Now;
for (int i = 0; i < 1000; i++)
{
    
string temp = StrFun.Sub(source, length);
}
ts 
= DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);

dt 

= DateTime.Now;
for (int i = 0; i < 1000; i++)
{
    
string temp = StrFun.GetStr(source, length);
}
ts 
= DateTime.Now - dt;
Console.WriteLine(ts.TotalMilliseconds);

Console.Read();

结果为:
2328.125
1515.625
15.625