JEphem Informatic Trail tig source code Strings.java
//*********************************************************************************
// 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 <CODE>String</CODE> 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 <CODE>String</CODE> in an array (a <CODE>String[]</CODE>).
  @param array Arry of Strings where String 's' is searched.
  @param s String we want to locate in <CODE>array</CODE>
  @return The position of <CODE>s</CODE> in <CODE>array</CODE>, 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;i<array.length;i++){
      if (s.compareTo(array[i])==0){
        found = true;
        break;
      }
    }
    if(found) return i;
    else return NO_SPECIF;
  }// end findIndex

  //******************* stringToStringArray(String s) **************
  /** Convenient method to call {@link #stringToStringArray(String, char)},
  using a coma (',') as separator. */
  public static String[] stringToStringArray(String s){ return stringToStringArray(s, ','); }

  //******************* stringToStringArray(String s, char separator) **************
  /**	Transforms a <CODE>String</CODE> in an array of <CODE>String</CODE>s.
  <BR><B>Example</B> : if <CODE>s = "arg1, arg2,arg3 "</CODE> and <CODE>separator = ','</CODE>,
  <CODE>stringToStringArray</CODE> returns :
  <TABLE BORDER><TR>
  <TD><FONT SIZE="-1">arg1</FONT></TD>
  <TD><FONT SIZE="-1">arg2</FONT></TD>
  <TD><FONT SIZE="-1">arg3</FONT></TD>
  </TR></TABLE>
  Points to consider :
  <LI>Leading and trailing spaces are removed (see <CODE>arg2</CODE> and <CODE>arg3</CODE>).</LI>
  <LI>Separators at the begining or at the end of <CODE>s</CODE> lead to creation
  of empty Strings in the resulting array.</LI>
  <LI>The same thing happens if two separators are side by side :
  <BR>
  if <CODE>s=",arg1,, arg2,arg3"</CODE>, <CODE>stringToStringArray</CODE> returns :
  <TABLE BORDER><TR>
  <TD><I><FONT SIZE="-1">(empty String)</FONT></I></TD>
  <TD><FONT SIZE="-1">arg1</FONT></TD>
  <TD><I><FONT SIZE="-1">(empty String)</FONT></I></TD>
  <TD><FONT SIZE="-1">arg2</FONT></TD>
  <TD><FONT SIZE="-1">arg3</FONT></TD>
  </TR></TABLE>
  </LI>

  @param s String to analyze.
  @param separator Character used as separator to analyze <CODE>s</CODE>.
  @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, détermination de la longueur du tableau réultat res[]
    while(sepPos!=-1){
      curPos=sepPos+1;
      sepPos=s.indexOf(separator, curPos);
      i++;
    }
    //2ème 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 <CODE>int[]</CODE> to a String.
  <BR>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 <CODE>int[]</CODE> ; in the <CODE>String</CODE>, the integers must be
  separated by comas (',').
  <BR>Example : if 'str' contains "1, 4, -9, 0", will be transfomed to
  <CODE>new int[]{1, 4, -9, 0}</CODE>.
  @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 <CODE>double[]</CODE> to a String.
  <BR>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 <CODE>double[]</CODE> ; in the <CODE>String</CODE>, the doubles must be
  separated by comas (',').
  <BR>Example : if 'str' contains "1, 4.02, -9.3, 0", will be transfomed to
  <CODE>new double[]{1.0, 4.02, -9.3, 0.0}</CODE>.
  @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 <CODE>java.lang.Double.parseDouble(String)</CODE>, but more permissive.
  The string is converted to a double if the separator is a point or a coma.
  <BR>Ex : <CODE>parseDouble("12.34")</CODE> and <CODE>parseDouble("12,34")</CODE> will return the double
  <CODE>12.34</CODE>.
  @param s The String that this method tries to convert into a <CODE>double</CODE>.
  @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 <CODE>java.lang.String.replace(char, char)</CODE>, 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