首页 > 代码库 > java中字符串切割的方法总结
java中字符串切割的方法总结
StringTokenizer最快 ,基本已经不用了,除非在某些需要效率的场合。
Scanner最慢。
String和Pattern速度差不多。Pattern稍快些。 String和Pattern的split 方法效率相当,常用
- public static void main(String [] args){
- long start = System.currentTimeMillis();
- for ( int i= 0 ;i< 100000 ;i++){
- test1();
- }
- long end = System.currentTimeMillis();
- System.out.println("StringTokenzier: " +(end-start));
- start = System.currentTimeMillis();
- for ( int i= 0 ;i< 100000 ;i++){
- test2();
- }
- end = System.currentTimeMillis();
- System.out.println("Scanner: " +(end-start));
- start = System.currentTimeMillis();
- for ( int i= 0 ;i< 100000 ;i++){
- test3();
- }
- end = System.currentTimeMillis();
- System.out.println("String: " +(end-start));
- start = System.currentTimeMillis();
- for ( int i= 0 ;i< 100000 ;i++){
- test4();
- }
- end = System.currentTimeMillis();
- System.out.println("Pattern: " +(end-start));
- }
- //用StringTokenizer
- public static void test1(){
- String input = "This is 1 just a test" ;
- StringTokenizer st = new StringTokenizer(input);
- while (st.hasMoreTokens()){
- String s = st.nextToken();
- }
- }
- //用Scanner的方法
- public static void test2(){
- String input = "This is 1 just a test" ;
- Scanner scanner = new Scanner(input);
- scanner.useDelimiter(" " );
- while (scanner.hasNext()){
- String s = scanner.next();
- }
- }
- //用String的split方法
- public static void test3(){
- String input = "This is 1 just a test" ;
- String [] str = input.split(" " );
- for ( int i= 0 ;i<str.length;i++){
- String s = str[i];
- }
- }
- //用Pattern的方法
- public static void test4(){
- Pattern pattern = Pattern.compile(" " );
- String input = "This is 1 just a test" ;
- String [] str = pattern.split(input);
- for ( int i= 0 ;i<str.length;i++){
- String s = str[i];
- }
- }
[java] view plaincopyprint?
- public static void main(String [] args){
- long start = System.currentTimeMillis();
- for(int i=0;i<100000;i++){
- test1();
- }
- long end = System.currentTimeMillis();
- System.out.println("StringTokenzier: "+(end-start));
- start = System.currentTimeMillis();
- for(int i=0;i<100000;i++){
- test2();
- }
- end = System.currentTimeMillis();
- System.out.println("Scanner: "+(end-start));
- start = System.currentTimeMillis();
- for(int i=0;i<100000;i++){
- test3();
- }
- end = System.currentTimeMillis();
- System.out.println("String: "+(end-start));
- start = System.currentTimeMillis();
- for(int i=0;i<100000;i++){
- test4();
- }
- end = System.currentTimeMillis();
- System.out.println("Pattern: "+(end-start));
- }
- //用StringTokenizer
- public static void test1(){
- String input = "This is 1 just a test";
- StringTokenizer st = new StringTokenizer(input);
- while(st.hasMoreTokens()){
- String s = st.nextToken();
- }
- }
- //用Scanner的方法
- public static void test2(){
- String input = "This is 1 just a test";
- Scanner scanner = new Scanner(input);
- scanner.useDelimiter(" ");
- while(scanner.hasNext()){
- String s = scanner.next();
- }
- }
- //用String的split方法
- public static void test3(){
- String input = "This is 1 just a test";
- String [] str = input.split(" ");
- for(int i=0;i<str.length;i++){
- String s = str[i];
- }
- }
- //用Pattern的方法
- public static void test4(){
- Pattern pattern = Pattern.compile(" ");
- String input = "This is 1 just a test";
- String [] str = pattern.split(input);
- for(int i=0;i<str.length;i++){
- String s = str[i];
- }
- }
运行了五次。输出结果如下:
Java代码
- StringTokenzier: 110
- Scanner: 3281
- String: 453
- Pattern: 438
- StringTokenzier: 109
- Scanner: 3297
- String: 453
- Pattern: 438
- StringTokenzier: 110
- Scanner: 3265
- String: 485
- Pattern: 437
- StringTokenzier: 109
- Scanner: 3266
- String: 469
- Pattern: 437
- StringTokenzier: 109
- Scanner: 3297
- String: 469
- Pattern: 422
[java] view plaincopyprint?
- StringTokenzier: 110
- Scanner: 3281
- String: 453
- Pattern: 438
- StringTokenzier: 109
- Scanner: 3297
- String: 453
- Pattern: 438
- StringTokenzier: 110
- Scanner: 3265
- String: 485
- Pattern: 437
- StringTokenzier: 109
- Scanner: 3266
- String: 469
- Pattern: 437
- StringTokenzier: 109
- Scanner: 3297
- String: 469
- Pattern: 422
StringTokenizer最快
Scanner最慢。
String和Pattern速度差不多。Pattern稍快些。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。