无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

split && StringTokenizer

Posted on 2009-07-25 15:50 Gavin.lee 阅读(174) 评论(0)  编辑  收藏 所属分类: java SE & EE

由于业务,需要经常地对数据分割,这两种都是用过,感觉split比StringTokenizer好用,究其原因,split是JDK1.4新功能,弥补了StringTokenizer,因此StringTokenizer也没更新过了。

public String[] split(String regex);

package com.Gavin.tools;
public class TestSplit {
    
public static void main(String[] args) {
        TestSplit ts 
= new TestSplit();
        System.out.println(ts.bubbleSort(
"1,32,23 14 5,7"));
    }

    
    
public static String bubbleSort(String str1) {
        String st[] 
= str1.split(",| ");
        
for (int i = 0; i < st.length; i++{
            
for (int j = 0; j < st.length - 1 - i; j++{
                
if (Integer.parseInt(st[j]) > Integer.parseInt(st[j + 1])) {
                    String temp 
= st[j];
                    st[j] 
= st[j + 1];
                    st[j 
+ 1= temp;
                }

            }

        }

        String str2 
= "";
        
for (int i = 0; i < st.length; i++{
            
if (str2.equals("")) {
                str2 
= st[i];
            }
 else {
                str2 
= str2 + "," + st[i];
            }

        }

        
return str2;
    }

}

//1,5,7,14,23,32


public StringTokenizer(String str, String delim);

package com.Gavin.tools;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;
public class TestStringTokenizer {    
    
private static Logger logger = Logger.getLogger(TestStringTokenizer.class);
    
public static void main(String[] args) {        
        StringTokenizer tokenizer 
= new StringTokenizer("I am a developer in shenzhen"" ");        
        logger.debug(tokenizer.countTokens());
        
        
while(tokenizer.hasMoreTokens()) {
            logger.debug(tokenizer.nextToken());
//            logger.debug(tokenizer.nextToken(" "));    //nextToken(String delim); 下一个分隔符分割的值
        }
        
    }

}

//[DEBUG      0  -line:15  -content:6
//[DEBUG      0  -line:19  -content:I
//[DEBUG     16  -line:19  -content:am
//[DEBUG     16  -line:19  -content:a
//[DEBUG     16  -line:19  -content:developer
//[DEBUG     16  -line:19  -content:in
//[DEBUG     16  -line:19  -content:shenzhen

只有注册用户登录后才能发表评论。


网站导航: