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

推荐订阅源

S
Security Affairs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Jina AI
Jina AI
P
Palo Alto Networks Blog
GbyAI
GbyAI
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Hugging Face - Blog
Hugging Face - Blog
小众软件
小众软件
Y
Y Combinator Blog
T
The Blog of Author Tim Ferriss
Blog — PlanetScale
Blog — PlanetScale
S
Schneier on Security
V
Vulnerabilities – Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
雷峰网
雷峰网
T
Tenable Blog
人人都是产品经理
人人都是产品经理
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
Scott Helme
Scott Helme
SecWiki News
SecWiki News
C
CERT Recently Published Vulnerability Notes
Recorded Future
Recorded Future
I
InfoQ
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
Cloudbric
Cloudbric
C
Check Point Blog
Engineering at Meta
Engineering at Meta
TaoSecurity Blog
TaoSecurity Blog
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
N
News and Events Feed by Topic
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
腾讯CDC
量子位
Application and Cybersecurity Blog
Application and Cybersecurity Blog
K
Kaspersky official blog
Vercel News
Vercel News
F
Full Disclosure
T
Troy Hunt's Blog
Forbes - Security
Forbes - Security
S
Security @ Cisco Blogs

博客园 - angushine

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

代码

package com;import java.util.Date;public class StrFun {
    
    
public static void main(String[] args) {
        String source 
= "123《中国》按时打发按时打发撒地方啊飒飒asdfasdfasd的法师打发似的<发(生的发生)地方按时打发是asdfasdfas";
        
int length = 10;
        Date dt1 
= new Date();
        String temp 
= null;
        
for(int i = 0; i < 1000; i ++) {
            temp 
= StrFun.omitString(source, length);
        }
        Date dt2 
= new Date();
        
long ts = dt2.getTime() - dt1.getTime();
        System.out.println(ts);
        System.out.println(temp);
    }
// 获取指定长度的字符串
    public static String omitString(String string, int length) {
        
int count = 0;
        
for (int i = 0; i < string.length(); i++) {
            
if (Integer.toHexString(string.charAt(i)).length() == 4) {
                count 
+= 2;
            } 
else {
                count
++;
            }
        }
        StringBuffer sb 
= new StringBuffer();
        
if (count > length) {
            count 
= 0;
            
for (int i = 0; i < string.length(); i++) {
                
char temp = string.charAt(i);
                
if (Integer.toHexString(temp).length() == 4) {
                    count 
+= 2;
                } 
else {
                    count
++;
                }
                
if (count < length) {
                    sb.append(temp);
                }
                
if (count == length) {
                    sb.append(temp);
                    
break;
                }
                
if (count > length) {
                    sb.append(
" ");
                    
break;
                }
            }
            sb.append(
"");
        } 
else {
            sb.append(string);
        }
        
return sb.toString();
    }
}

posted @ 2010-01-07 20:28  angushine  阅读(281)  评论()    收藏  举报