本文发表在 rolia.net 枫下论坛package com.bgi.util;
import java.util.*;
public class Spliter {
public Spliter() {
}
private static String nameOfThisClass = "Spliter";
public final static String DELIMITER = "|";
public static String[] split(String s){
return split(s,DELIMITER);
}
public static String[] split(String s, String delimiter){
checkSource(s);
int delimiterLength;int stringLength = s.length();
if (delimiter == null || (delimiterLength = delimiter.length()) == 0){.
return new String[] {s};
}
int count;
int start;
int end;
char slash = '\\';
// Scan s and count the tokens.
count = 0;
start = 0;
while((end = s.indexOf(delimiter, start)) != -1) {
if (s.charAt(end-1)!=slash){
count++;
start = end + delimiterLength;
} else {
start = end + delimiterLength;
continue;
}}
count++;
String[] result = new String[count];
count = 0;
start = 0;
while((end = s.indexOf(delimiter, start)) != -1) {
if(s.charAt(end-1)!=slash){
result[count] = (s.substring(start, end));
count++;
start = end + delimiterLength;
} else {
start = end + delimiterLength;
continue;
}}
end = stringLength;
result[count] = s.substring(start, end);
return (result);
}
/**
* Check if the input parameter is null
*
* @throws IllegalArgumentException if source is null
*/
private static void checkSource(String source)
throws IllegalArgumentException
{
if ( source == null ){
System.out.println( nameOfThisClass + "The input string is null..");
throw new IllegalArgumentException();
}
}
/**
* Test method
*/
public static void main(String args[]){
try{
String a[]=Spliter.split(args[0]);
int i;
for (i=0; i<a.length; i++ ){
System.out.print(a[i]+"^");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net
import java.util.*;
public class Spliter {
public Spliter() {
}
private static String nameOfThisClass = "Spliter";
public final static String DELIMITER = "|";
public static String[] split(String s){
return split(s,DELIMITER);
}
public static String[] split(String s, String delimiter){
checkSource(s);
int delimiterLength;int stringLength = s.length();
if (delimiter == null || (delimiterLength = delimiter.length()) == 0){.
return new String[] {s};
}
int count;
int start;
int end;
char slash = '\\';
// Scan s and count the tokens.
count = 0;
start = 0;
while((end = s.indexOf(delimiter, start)) != -1) {
if (s.charAt(end-1)!=slash){
count++;
start = end + delimiterLength;
} else {
start = end + delimiterLength;
continue;
}}
count++;
String[] result = new String[count];
count = 0;
start = 0;
while((end = s.indexOf(delimiter, start)) != -1) {
if(s.charAt(end-1)!=slash){
result[count] = (s.substring(start, end));
count++;
start = end + delimiterLength;
} else {
start = end + delimiterLength;
continue;
}}
end = stringLength;
result[count] = s.substring(start, end);
return (result);
}
/**
* Check if the input parameter is null
*
* @throws IllegalArgumentException if source is null
*/
private static void checkSource(String source)
throws IllegalArgumentException
{
if ( source == null ){
System.out.println( nameOfThisClass + "The input string is null..");
throw new IllegalArgumentException();
}
}
/**
* Test method
*/
public static void main(String args[]){
try{
String a[]=Spliter.split(args[0]);
int i;
for (i=0; i<a.length; i++ ){
System.out.print(a[i]+"^");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}更多精彩文章及讨论,请光临枫下论坛 rolia.net