import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
public final class StringUtil {
/**
* Replaces all the occurences of the Apostrophe in transStr
* with double Apostrophe.
* String manipulation needed before saving to the database.
*
* @param transStr String to be transformed
* @return Transformed string
*/
public static String transformedName(String transStr) {
String cApostrophe = "''";
//
String newLine = "\n";
String transName = transStr.trim();
transName = replaceString(transName, "'", cApostrophe);
return transName;
}
/**
* Replaces all the occurences of the oldStr in the originalStr with the newStr.
*
* @param originalStr String to be modified
* @param oldStr String to be replaced
* @param newStr New string
* @return Modified string
*/
public static String replaceString( String originalStr, String oldStr,
String newStr) {
int j = newStr.indexOf(oldStr);
boolean inside = false;
if (j != -1)
inside = true;
int i =originalStr.indexOf(oldStr);
while (i != -1) {
if (i == originalStr.length() - 1)
originalStr = originalStr.substring(0, i) + newStr;
else
originalStr = originalStr.substring(0, i) + newStr
+ originalStr.substring( i + oldStr.length());
if (inside)
i = originalStr.indexOf(oldStr, i + newStr.length());
else
i = originalStr.indexOf(oldStr);
}
return originalStr;
}
/**
* Trims only the right side of the String.
*
* @param originalStr String to be modified
* @return Modified string
*/
public static String rightTrim( String originalStr ) {
int len = originalStr.length();
int st = 0;
char[] val = originalStr.toCharArray();
while ((st < len) && (val[len - 1] <= ' ')) {
len--;
}
String newStr = originalStr.substring(st, len);
return newStr;
}
/**
* @return a formatted string with first letter Capital and the rest lower case
*/
public static String formatProperCase(String orgString) {
return (orgString.substring(0,1)).toUpperCase() + orgString.substring(1);
}
/**
* Transformes a given string into a date object using a specified date format.
* If an exception occurs an 'NULL' value is returned.
*
* @param date String to be transformed into Date object
* @return Result of the transormation
*/
public static Date parseDateString(String date) {
DateFormat dFormat = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
try {
d = dFormat.parse(date);
} catch (ParseException e) {
System.out.println("ParseException on dateString - " + date + " : " + e.getMessage());
d = null;
}
return d;
}
/**
* Transformes a date formated string by reversing the elements,
* e.g. if the string looks like yy/mm/dd the result will be dd/mm/yy.
*
* @param date String to be transformed
* @return new String created
*/
public static String changeDateStringFormat(String date) {
if (date.trim().length() != 0) {
StringTokenizer str = new StringTokenizer(date, "/");
String s1 = str.nextToken();
String s2 = str.nextToken();
String s3 = str.nextToken();
return s3 + "/" + s2 + "/" + s1;
}
return date;
}
/**
* Transforms the original text according to agreed rules.
* Usually changes first letter to upper case and the rest to lower case.
*/
public static String properCase(String originalText) {
String text = originalText.trim();
if(text.length() == 0)
return "";
String properC = (text.substring(0,1)).toUpperCase()
+ (text.substring(1)).toLowerCase();
//2009-10-29 gaofei add
if(text.endsWith("'") || text.endsWith("*") || text.endsWith("_")){
return properC;
}
//------------
int i = 0;
while (i < properC.length()) {
if ((properC.substring(i,i+1)).equals("'")) {
i = i + 1;
properC = properC.substring(0, i)
+ (properC.substring(i,i + 1)).toUpperCase()
+ properC.substring(i+1);
i = i - 1;
}
if ((properC.substring(i,i+1)).equals(" ")) {
i = i + 1;
properC = properC.substring(0, i)
+ (properC.substring(i,i+1)).toUpperCase()
+ properC.substring(i+1);
i = i - 1;
}
if ((properC.substring(i,i+1)).equals("*")) {
i = i + 1;
properC = properC.substring(0, i-1)
+ (properC.substring(i,i+1)).toUpperCase()
+ properC.substring(i+1);
i = i - 1;
}
if ((properC.substring(i,i+1)).equals("_")) {
i = i + 1;
properC = properC.substring(0, i-1)
+ (properC.substring(i,i+1)).toLowerCase()
+ properC.substring(i+1);
i = i - 1;
}
i++;
}
return properC = properC.trim();
}
}