Various String Manipulation Utilities
/**
* Checks whether the first character of a string is an alphabetic character.
*
* @param sString
* @return
*/
public static boolean IsAlpha(String sString){
return Character.isAlphabetic(sString.charAt(0));
}
/**
* Checks whether the first character of a string is a digit.
*
* @param sString
* @return
*/
public static boolean IsDigit(String sString){
return Character.isDigit(sString.charAt(0));
}
/**
* Checks whether the first character of a string is a space.
*
* @param sString
* @return
*/
@SuppressWarnings("deprecation")
public static boolean IsSpace(String sString){
return Character.isSpace(sString.charAt(0));
}
/**
* Replicates a string to the specified number of times.
*
* @param sOrig
* @param iCopies
* @return
*/
public static String Replicate(String sOrig, int iCopies){
String repeated = null;
if(iCopies == 0){
repeated = sOrig;
}
else{
repeated = new String(new char[iCopies]).replace("\0", sOrig);
}
return repeated;
}
/**
* Returns the position of the specified substring.
*
* @param sSubstr
* @param sTarget
* @return
*/
public static int StrPos(String sSubstr, String sTarget){
return (sTarget.indexOf(sSubstr) + 1);
}
/**
* A function which returns the position of the specified substring counting from right direction.
*
* @param sSubstr
* @param sTarget
* @param bBackward
* @return
*/
public static int StrPos(String sSubstr, String sTarget, boolean bBackward){
int pos = 0;
if(bBackward == true)
pos = (sTarget.lastIndexOf(sSubstr) + 1);
else
pos = (sTarget.indexOf(sSubstr) + 1);
return pos;
}
For example:
String text = "one and two and three";
System.out.println(StringManipulationUtils.StrPos("and", text, false));
It prints : 5
System.out.println(StringManipulationUtils.StrPos("and", text, true));
It prints : 13
/**
* Searches for and replaces characters in a string.
*
* @param sOrig
* @param sSearch
* @param sReplace
* @return
*/
public static String StrTran(String sOrig, String sSearch, String sReplace){
return sOrig.replaceAll(sSearch, sReplace);
}
For example:
String text = "one and two and three";
System.out.println(StringManipulationUtils.StrTran(text, "and", "&"));
It prints : one & two & three
* Inserts and deletes characters.
*
* @param sOrig
* @param iPos
* @param iLen
* @param sReplace
* @return
*/
public static String Stuff(String sOrig, int iPos, int iLen, String sReplace){
StringBuilder strBuild = new StringBuilder(sOrig);
if(null != sReplace && sReplace.length() == 0)
strBuild.replace(iPos - 1, sOrig.length(), sReplace);
if(iLen == 0){
strBuild.insert(iPos - 1, sReplace);
}
if(iLen == sReplace.length()){
strBuild.replace(iPos - 1, sOrig.length(), sReplace);
}
if(sReplace.length()< iLen){
strBuild.replace(iPos - 1, iLen, sReplace);
}
if(iLen >= sOrig.length()){
strBuild.replace(iPos - 1, sOrig.length(), sReplace);
}
return strBuild.toString();
}
For example: -
String s3 = "hello there";
System.out.println(StringManipulationUtils.Stuff(s3,7,0,"OUT "));
It prints : hello OUT there
System.out.println(StringManipulationUtils.Stuff(s3,7,5,"WORLD"));
It prints : hello WORLD
System.out.println(StringManipulationUtils.Stuff(s3,6,6,""));
It prints : hello
System.out.println(StringManipulationUtils.Stuff(s3,1,5,"HERE"));
It prints : HERE there
System.out.println(StringManipulationUtils.Stuff(s3,1,11,"WELCOME"));
It prints : WELCOME
/**
* Returns a substring.
*
* @param sString
* @param iPos
* @param iLen
* @return
*/
public static String SubStr(String sString, int iPos, int iLen){
return sString.substring(iPos - 1, (iPos - 1) + iLen);
}
For Example:-
String sText = "How much wood would a woodchuck chuck?";
System.out.println(StringManipulationUtils.SubStr(sText, 23, 9));
It prints : woodchuck
No comments:
Post a Comment