//********************************************************************************* // 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 forin an array ofStringmanipulation. @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 aStringin an array (aString[]). @param array Arry of Strings where String 's' is searched. @param s String we want to locate inarray@return The position ofsinarray, 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 Strings.
Example : ifs = "arg1, arg2,arg3 "andseparator = ',',stringToStringArrayreturns :
| arg1 | arg2 | arg3 |
arg2 and arg3).s lead to creation
of empty Strings in the resulting array.s=",arg1,, arg2,arg3", stringToStringArray returns :
| (empty String) | arg1 | (empty String) | arg2 | arg3 |
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.
int[] ; in the String, the integers must be
separated by comas (',').
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.
double[] ; in the String, the doubles must be
separated by comas (',').
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.
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.