//*********************************************************************************
// class tig.Strings
// Software released under the General Public License (version 2 or later), available at
// http://www.gnu.org/copyleft/gpl.html
//*********************************************************************************
package tig;

import tig.GeneralConstants;
/**********************************************************************************
Class containing utility static methods for String manipulation.
@author Thierry Graff
@history sep 24 2001 : creation from an old class (Util.java).

@todo Check parameter in StringToStringArray ===> IllegalArgumentException
**********************************************************************************/
public abstract class Strings implements GeneralConstants{

  //=================================================================================
  //                                PUBLIC METHODS
  //=================================================================================

  //***************** findIndex ****************************
  /** Finds the position of a String in an array (a String[]).
  @param array Arry of Strings where String 's' is searched.
  @param s String we want to locate in array
  @return The position of s in array, or {@link tig.GeneralConstants#NO_SPECIF} if not found.
  */
  public static int findIndex(String[] array, String s){
    boolean found = false;
    int i;
    for(i=0;iString in an array of Strings.
  
Example : if s = "arg1, arg2,arg3 " and separator = ',', stringToStringArray returns :
arg1 arg2 arg3
Points to consider :
  • Leading and trailing spaces are removed (see arg2 and arg3).
  • Separators at the begining or at the end of s lead to creation of empty Strings in the resulting array.
  • The same thing happens if two separators are side by side :
    if s=",arg1,, arg2,arg3", stringToStringArray returns :
    (empty String) arg1 (empty String) arg2 arg3
  • @param s String to analyze. @param separator Character used as separator to analyze s. @return An array of Strings resulting from the analysis. *****************************************************************/ public static String[] stringToStringArray(String s, char separator){ //System.out.println("s = " + s); int curPos=0, i=0; int sepPos=s.indexOf(separator, curPos); //1er passage, determination de la longueur du tableau reultat res[] while(sepPos!=-1){ curPos=sepPos+1; sepPos=s.indexOf(separator, curPos); i++; } //2eme passage, remplissage de res[] //System.out.println("2eme passage"); String[] res = new String[i+1]; curPos=0; i=0; sepPos=s.indexOf(separator, curPos); while(sepPos!=-1){ res[i]=s.substring(curPos, sepPos).trim(); //System.out.println("res[" + i + "] = " + res[i] + " ; sepPos = " + sepPos); curPos=sepPos+1; sepPos=s.indexOf(separator, curPos); i++; } res[i]=s.substring(curPos, s.length()).trim(); //System.out.println("res[" + i + "] = " + res[i] + " ; sepPos = " + sepPos); return res; }//end stringToStringArray //***************** intArrayToString **************************** /** Converts a int[] to a String.
    Example : if 'intArray' contains 1, 4, -9 and 0 will be transfomed to "1, 4, -9, 0". */ public static String intArrayToString(int[] intArray){ String strRes = BLANK; boolean first = true; String COMA = ", "; for (int i = 0; i < intArray.length; i++){ if (!first) strRes += COMA; else first = false; strRes += intArray[i]; } return strRes; }// end intArrayToString //***************** stringToIntArray **************************** /** Converts a String to an int[] ; in the String, the integers must be separated by comas (',').
    Example : if 'str' contains "1, 4, -9, 0", will be transfomed to new int[]{1, 4, -9, 0}. @throws NumberFormatException If elements of 'str' can't be converted to integers. */ public static int[] stringToIntArray(String str){ String[] values = Strings.stringToStringArray(str); int[] res = new int[values.length]; for (int i = 0; i < values.length; i++){ res[i] = Integer.parseInt(values[i]); } return res; }// end stringToIntArray //***************** doubleArrayToString **************************** /** Converts a double[] to a String.
    Example : if 'doubleArray' contains 1.0, 0.0 and -9.5 will be transfomed to "1.0, 0.0, -9.5". */ public static String doubleArrayToString(double[] doubleArray){ String strRes = BLANK; boolean first = true; String COMA = ", "; for (int i = 0; i < doubleArray.length; i++){ if (!first) strRes += COMA; else first = false; strRes += doubleArray[i]; } return strRes; }// end doubleArrayToString //***************** stringToDoubleArray **************************** /** Converts a String to an double[] ; in the String, the doubles must be separated by comas (',').
    Example : if 'str' contains "1, 4.02, -9.3, 0", will be transfomed to new double[]{1.0, 4.02, -9.3, 0.0}. @throws NumberFormatException If elements of 'str' can't be converted to doubles. */ public static double[] stringToDoubleArray(String str){ String[] values = Strings.stringToStringArray(str); double[] res = new double[values.length]; for (int i = 0; i < values.length; i++){ res[i] = Double.parseDouble(values[i]); } return res; }// end stringToDoubleArray //***************** parseDouble **************************** /** Equivalent to java.lang.Double.parseDouble(String), but more permissive. The string is converted to a double if the separator is a point or a coma.
    Ex : parseDouble("12.34") and parseDouble("12,34") will return the double 12.34. @param s The String that this method tries to convert into a double. @throws NumberFormatException If 's' can't be parsed. */ public static double parseDouble(String s){ double res; try{ res = Double.parseDouble(s); return res; } catch (NumberFormatException nfe){ s = s.replace(',', '.'); try{ res = Double.parseDouble(s); return res; } catch (NumberFormatException nfe2){ throw nfe2; } } }// end parseDouble //***************** replace **************************** /** All occurences of 's1' contained in 'theString' are replaced by s2. Equivalent to java.lang.String.replace(char, char), but with Strings. @param theString String to be modified. @param s1 substring of 'theString' to be replaced by 's2'. @param s2 String used to replace 's1' in 'theString'. @return 'theString', modified as specified above. */ public static String replace(String theString, String s1, String s2){ String res = BLANK; int len1 = s1.length(); int i = theString.indexOf(s1); while(i != -1){ res += theString.substring(0, i) + s2; theString = theString.substring(i + len1); i = theString.indexOf(s1); } res += theString; return res; }// end replace //================================================================================= //================================================================================= // TESTS //================================================================================= //================================================================================= /* // **************** For tests only **************** //D:\Programs\java\jdk1.4\bin\java -classpath .;bin tig.Strings testReplace aabbccaabbcc cc zaaz public static void main(String[] args){ // no complete argument checking if(args[0].equalsIgnoreCase("testStringToIntArray")) testStringToIntArray(args[1]); else if(args[0].equalsIgnoreCase("testStringToDoubleArray")) testStringToDoubleArray(args[1]); else if(args[0].equalsIgnoreCase("testDoubleArrayToString")) testDoubleArrayToString(); else if(args[0].equalsIgnoreCase("testReplace")) testReplace(args[1], args[2], args[3]); else{ String possibleArgs = "'testStringToIntArray' or 'testStringToDoubleArray' or 'testDoubleArrayToString' or " + "'testReplace'"; System.out.println("first argument must be " + possibleArgs); } }// end main // **************** For tests only **************** private static void testStringToIntArray(String str){ int[] res = stringToIntArray(str); for(int i = 0; i < res.length; i++) System.out.println(res[i]); }// end testStringToIntArray // **************** For tests only **************** // Call : tig.Strings testStringToDoubleArray 1,5.02,0,2,4.5 private static void testStringToDoubleArray(String str){ double[] res = stringToDoubleArray(str); for(int i = 0; i < res.length; i++) System.out.println(res[i]); }// end testStringToIntArray // **************** For tests only **************** private static void testDoubleArrayToString(){ double[] dbl = {1.3, 1.55, 0.0, 1.0}; System.out.println("res = " + doubleArrayToString(dbl)); }// end testDoubleArrayToString // **************** For tests only **************** //D:\Programs\java\jdk1.4\bin\java -classpath .;bin tig.Strings testReplace tresesA eses zz private static void testReplace(String theString, String s1, String s2){ System.out.println("s1 = " + s1); System.out.println("s2 = " + s2); System.out.println("res = " + replace(theString, s1, s2)); }// end testDoubleArrayToString */ }//end abstract class Strings

    Ads help maintain this website.