熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

開發JAVA編程中字符串分割的兩種方法

2013-11-23 19:42:42  來源: Java高級技術 

  方法:采用String的split驗證代碼如下:

  import javautilArrays;

  public class TestSplit {

  public static void main(String[] args) {

  String orignString = new String();

  String[] testString = orignStringsplit();

  int[] test = { };

  //String to int

  for (int i = ; i < testStringlength; i++) {

  test[i] = IntegerparseInt(testString[i]);

  }

  //sort

  Arrayssort(test);

  //asc sort

  for (int j = ; j < testlength; j++) {

  Systemoutprintln(test[j]);

  }

  Systemoutprintln(next );

  //  desc

  for (int i = (testlength ); i >= ; i) {

  Systemoutprintln(test[i]);

  }

  }

  }

  方法:采用StringTokenizer

  import javautilArrays;

  import javautilStringTokenizer;

  public class SplitStringTest {

  public static void main(String[] args) {

  String s = new String();

  int length = slength();

  //split   s with

  StringTokenizer commaToker = new StringTokenizer(s );

  String[] result = new String[untTokens()];

  int k = ;

  while (commaTokerhasMoreTokens()) {

  result[k] = commaTokernextToken();

  k++;

  }

  int[] a = new int[resultlength];

  for (int i = ; i < resultlength; i++) {

  a[i] = IntegerparseInt(result[i]);

  }

  //sort

  Arrayssort(a);

  //asc sort

  for (int j = ; j < resultlength; j++) {

  Systemoutprintln(a[j]);

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/gj/201311/27344.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.