基于指定数组大小的切割字符串(java字符串分割性能比较)
2013-04-13 18:56
866 查看
参看:http://kfyfly.iteye.com/blog/1105716
/**
* 基于指定数组大小的切割字符串
* @param str
* @param tag
* @param arraySize
* @return
*/
public static String[] split(String str,String tag,int arraySize){
String[] result = new String[arraySize];
int k=0,count=0;
for (int i = 0; i < str.length(); i++) {
if(str.substring(i,i+1).equals(tag)){
if(count==0){
result[count] = str.substring(0, i);
}else if(count==(arraySize-2)){//如果是倒数第二个元素
result[count] = str.substring(k+1, i);
result[arraySize-1] = str.substring(i+1, str.length());
}else{
result[count] = str.substring(k+1, i);
}
k=i;count++;
}
}
return result;
}
/**
* 基于指定数组大小的切割字符串
* @param str
* @param tag
* @param arraySize
* @return
*/
public static String[] split(String str,char tag,int arraySize){
String[] result = new String[arraySize];
int k=0,count=0;
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)==(tag)){
if(count==0){
result[count] = str.substring(0, i);
}else if(count==(arraySize-2)){//如果是倒数第二个元素
result[count] = str.substring(k+1, i);
result[arraySize-1] = str.substring(i+1, str.length());
}else{
result[count] = str.substring(k+1, i);
}
k=i;count++;
}
}
return result;
}
测试:
package com.dlmu.database.manager.test;
import com.dlmu.database.manager.util.Util;
public class TestSplit {
/**
* @param args
*/
public static void main(String[] args) {
String str = "sfsdf aaaaaaa ssssssssss rrrrrrrrr";
String str1 = "sfsdf bbbbbbbbb ccccccccccc eeeeeeeee";
String[] temp = null;
long start = System.currentTimeMillis();
for(int i = 0;i < 1000000;++i){
temp = Util.split(str," ",4);
}
System.out.println("string take time:"+(System.currentTimeMillis()-start));
System.out.println("temp[0]-------"+temp[0]);
System.out.println("temp[1]-------"+temp[1]);
System.out.println("temp[2]-------"+temp[2]);
System.out.println("temp[3]-------"+temp[3]);
String[] temp2 = null;
long start1 = System.currentTimeMillis();
for(int i = 0;i < 1000000;++i){
temp2 = Util.split(str,' ',4);
}
System.out.println("char take time:"+(System.currentTimeMillis()-start1));
System.out.println("temp2[0]-------"+temp2[0]);
System.out.println("temp2[1]-------"+temp2[1]);
System.out.println("temp2[2]-------"+temp2[2]);
System.out.println("temp2[3]-------"+temp2[3]);
String[] temp1 = Util.split(str1," ",4);
System.out.println("temp1[0]-------"+temp1[0]);
System.out.println("temp1[1]-------"+temp1[1]);
System.out.println("temp1[2]-------"+temp1[2]);
System.out.println("temp1[3]-------"+temp1[3]);
}
}
结果:
string take time:2313
temp[0]-------sfsdf
temp[1]-------aaaaaaa
temp[2]-------ssssssssss
temp[3]-------rrrrrrrrr
char take time:594
temp2[0]-------sfsdf
temp2[1]-------aaaaaaa
temp2[2]-------ssssssssss
temp2[3]-------rrrrrrrrr
temp1[0]-------sfsdf
temp1[1]-------bbbbbbbbb
temp1[2]-------ccccccccccc
temp1[3]-------eeeeeeeee
仅供参考
/**
* 基于指定数组大小的切割字符串
* @param str
* @param tag
* @param arraySize
* @return
*/
public static String[] split(String str,String tag,int arraySize){
String[] result = new String[arraySize];
int k=0,count=0;
for (int i = 0; i < str.length(); i++) {
if(str.substring(i,i+1).equals(tag)){
if(count==0){
result[count] = str.substring(0, i);
}else if(count==(arraySize-2)){//如果是倒数第二个元素
result[count] = str.substring(k+1, i);
result[arraySize-1] = str.substring(i+1, str.length());
}else{
result[count] = str.substring(k+1, i);
}
k=i;count++;
}
}
return result;
}
/**
* 基于指定数组大小的切割字符串
* @param str
* @param tag
* @param arraySize
* @return
*/
public static String[] split(String str,char tag,int arraySize){
String[] result = new String[arraySize];
int k=0,count=0;
for (int i = 0; i < str.length(); i++) {
if(str.charAt(i)==(tag)){
if(count==0){
result[count] = str.substring(0, i);
}else if(count==(arraySize-2)){//如果是倒数第二个元素
result[count] = str.substring(k+1, i);
result[arraySize-1] = str.substring(i+1, str.length());
}else{
result[count] = str.substring(k+1, i);
}
k=i;count++;
}
}
return result;
}
测试:
package com.dlmu.database.manager.test;
import com.dlmu.database.manager.util.Util;
public class TestSplit {
/**
* @param args
*/
public static void main(String[] args) {
String str = "sfsdf aaaaaaa ssssssssss rrrrrrrrr";
String str1 = "sfsdf bbbbbbbbb ccccccccccc eeeeeeeee";
String[] temp = null;
long start = System.currentTimeMillis();
for(int i = 0;i < 1000000;++i){
temp = Util.split(str," ",4);
}
System.out.println("string take time:"+(System.currentTimeMillis()-start));
System.out.println("temp[0]-------"+temp[0]);
System.out.println("temp[1]-------"+temp[1]);
System.out.println("temp[2]-------"+temp[2]);
System.out.println("temp[3]-------"+temp[3]);
String[] temp2 = null;
long start1 = System.currentTimeMillis();
for(int i = 0;i < 1000000;++i){
temp2 = Util.split(str,' ',4);
}
System.out.println("char take time:"+(System.currentTimeMillis()-start1));
System.out.println("temp2[0]-------"+temp2[0]);
System.out.println("temp2[1]-------"+temp2[1]);
System.out.println("temp2[2]-------"+temp2[2]);
System.out.println("temp2[3]-------"+temp2[3]);
String[] temp1 = Util.split(str1," ",4);
System.out.println("temp1[0]-------"+temp1[0]);
System.out.println("temp1[1]-------"+temp1[1]);
System.out.println("temp1[2]-------"+temp1[2]);
System.out.println("temp1[3]-------"+temp1[3]);
}
}
结果:
string take time:2313
temp[0]-------sfsdf
temp[1]-------aaaaaaa
temp[2]-------ssssssssss
temp[3]-------rrrrrrrrr
char take time:594
temp2[0]-------sfsdf
temp2[1]-------aaaaaaa
temp2[2]-------ssssssssss
temp2[3]-------rrrrrrrrr
temp1[0]-------sfsdf
temp1[1]-------bbbbbbbbb
temp1[2]-------ccccccccccc
temp1[3]-------eeeeeeeee
仅供参考
相关文章推荐
- Java数组拼接字符串几个写法性能比较
- 华为机试题: 将数组中的字符串按指定长度重新分割(java)
- java字符串分割性能比较
- oracle怎么切割一个指定的字符串按指定的标志符分割成字符数组
- Java数组拼接字符串几个写法性能比较(转载)
- Java数组拼接字符串几个写法性能比较
- java字符串分割性能比较
- java字符串分割性能比较
- java几种切割字符串方法的性能比较
- Java 5种字符串拼接方式性能比较
- Java字符串转换为日期和时间比较大小
- json-lib和jackson进行Java对象到json字符串序列化性能比较
- 按指定的字符把字符串分割为字符串数组
- Java中字符串相等与大小比较
- java中两个字符串如何比较大小
- java中subString、split、stringTokenizer三种截取字符串方法的性能比较
- Java将字符串按指定长度分割
- php把字符串指定字符分割成数组
- JAVA中 我要将字符串以空格为标记分割成多个字符串,并将分割后的字符串设为一个字符串数组