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

推荐订阅源

P
Proofpoint News Feed
WordPress大学
WordPress大学
Help Net Security
Help Net Security
Jina AI
Jina AI
Security Latest
Security Latest
Y
Y Combinator Blog
Project Zero
Project Zero
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Know Your Adversary
Know Your Adversary
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
Cisco Talos Blog
Cisco Talos Blog
博客园 - 司徒正美
MyScale Blog
MyScale Blog
Cyberwarzone
Cyberwarzone
D
Docker
T
The Blog of Author Tim Ferriss
G
Google Developers Blog
C
CERT Recently Published Vulnerability Notes
B
Blog
L
LangChain Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
The Hacker News
The Hacker News
C
Check Point Blog
L
Lohrmann on Cybersecurity
V2EX - 技术
V2EX - 技术
S
Securelist
T
Threat Research - Cisco Blogs
Stack Overflow Blog
Stack Overflow Blog
TaoSecurity Blog
TaoSecurity Blog
云风的 BLOG
云风的 BLOG
Latest news
Latest news
人人都是产品经理
人人都是产品经理
L
LINUX DO - 最新话题
Application and Cybersecurity Blog
Application and Cybersecurity Blog
The Register - Security
The Register - Security
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
AWS News Blog
AWS News Blog
C
Cybersecurity and Infrastructure Security Agency CISA
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
小众软件
小众软件
T
Tailwind CSS Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
宝玉的分享
宝玉的分享
O
OpenAI News

博客园 - 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)  评论()    收藏  举报