com.fathzer.soft.ajlib.utilities.StringUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ajlib Show documentation
Show all versions of ajlib Show documentation
A-Jlib is a simple java library with Swing widgets, utilities
and other stuff
package com.fathzer.soft.ajlib.utilities;
import java.util.LinkedList;
import java.util.List;
/** Utility to parse the fields separated by a char in a string.
* @author Jean-Marc Astesana
*
License: LGPL v3
*/
public abstract class StringUtils {
public static final String EMPTY = ""; //$NON-NLS-1$
private StringUtils() {
super();
}
/** Splits a string into fields.
*
The main advantage vs String#split is that the developer has not to deal with separators that
* are reserved characters of the regular expressions syntax
*
Some examples:
* - split("",',') -> {""}
* - split(",a,",',') -> {"","a",""}
*
* @param string The String to split
* @param separator The field separator
* @return an array of Strings.
*/
public static String[] split(String string, char separator) {
List result = new LinkedList();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i)==separator) {
result.add(buffer.toString());
if (buffer.length()>0) {
buffer.delete(0, buffer.length());
}
} else {
buffer.append(string.charAt(i));
}
}
result.add(buffer.toString());
return result.toArray(new String[result.size()]);
}
}