Monday, March 9, 2015

String Buffer Trim in a java pgm



String Buffer Trim

public class JavaStringBufferTrimExample
{
public static void main(String[] args)
{
 StringBuffer sbf = new StringBuffer("   Hello World  !  ");
 String str = sbf.toString().trim();
System.out.println("StringBuffer trim: \"" + str +"\"");
System.out.println("\"" + trim(sbf) + "\"");
}
private static String trim(StringBuffer sbf)
{
int start, end;
for(start = 0; start < sbf.length(); start++)
{
if(sbf.charAt(start) != ' ')
break;
}
for(end = sbf.length(); end > start ; end--)
{
if(sbf.charAt(end-1) != ' ')
break;
}
return sbf.substring(start, end);
    }
}
   

No comments:

Post a Comment