点击这里给我发消息 点击这里给我发消息

开发JAVA编程中字符串分割的两种方法

添加时间:2013-12-7
    相关阅读: 开发

  方法1:采用String的split,验证代码如下:

  import java.util.Arrays;

  public class TestSplit {

  public static void main(String[] args) {

  String orignString = new String("5,8,7,4,3,9,1");

  String[] testString = orignString.split(",");

  int[] test = { 0, 0, 0, 0, 0, 0, 0 };

  //String to int

  for (int i = 0; i < testString.length; i++) {

  test[i] = Integer.parseInt(testString[i]);

  }

  //sort

  Arrays.sort(test);

  //asc sort

  for (int j = 0; j < test.length; j++) {

  System.out.println(test[j]);

  }

  System.out.println("next ");

  //  desc

  for (int i = (test.length - 1); i >= 0; i--) {

  System.out.println(test[i]);

  }

  }

  }

  方法2:采用StringTokenizer

  import java.util.Arrays;

  import java.util.StringTokenizer;

  public class SplitStringTest {

  public static void main(String[] args) {

  String s = new String("5,8,7,4,3,9,1");

  int length = s.length();

  //split   s with ","

  StringTokenizer commaToker = new StringTokenizer(s, ",");

  String[] result = new String[commaToker.countTokens()];

  int k = 0;

  while (commaToker.hasMoreTokens()) {

  result[k] = commaToker.nextToken();

  k++;

  }

  int[] a = new int[result.length];

  for (int i = 0; i < result.length; i++) {

  a[i] = Integer.parseInt(result[i]);

  }

  //sort

  Arrays.sort(a);

  //asc sort

  for (int j = 0; j < result.length; j++) {

  System.out.println(a[j]);

  }

  }

  }

咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册