All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fathzer.soft.ajlib.utilities.StringUtils Maven / Gradle / Ivy

Go to download

A-Jlib is a simple java library with Swing widgets, utilities and other stuff

There is a newer version: 0.3.16
Show newest version
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()]); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy