elegant elegant - 3 months ago 18
Android Question

Replace semi colon (;) character in java


I am trying to truncate semi colon from both the ends of a string using this method:

private String truncateSemicolon(String inputString){
String outputString=inputString;
boolean isTruncated = false;
Log.i(TAG, "*************I N P U T T O T R U N C A T E ************* "+outputString);
while(!isTruncated){
if(outputString.startsWith(";")){

outputString=outputString.substring(1,outputString.length());
Log.i(TAG, "START *************************************"+outputString);
}
if(outputString.endsWith(";")){

outputString=outputString.substring(0,(outputString.length()-1));
Log.i(TAG, "************************************* END"+outputString);
}
if(!outputString.endsWith(";") && !outputString.startsWith(";")){
isTruncated=true;
}
}
Log.i(TAG, "*************O U T P U T T O T R U N C A T E ************* "+outputString);
return outputString;
}


But this truncates from beginning only.
What is the problem??

Answer

Your code is working fine you just follow a simple step:

String outputString=inputString.trim();