Added a method for padding a string to a certain width.

git-svn-id: https://samskivert.googlecode.com/svn/trunk@607 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
mdb
2002-02-23 18:24:02 +00:00
parent 491ab3f9c3
commit 46b8947354
@@ -1,5 +1,5 @@
//
// $Id: StringUtil.java,v 1.30 2002/02/17 23:35:15 mdb Exp $
// $Id: StringUtil.java,v 1.31 2002/02/23 18:24:02 mdb Exp $
//
// samskivert library - useful routines for java programs
// Copyright (C) 2001 Michael Bayne
@@ -70,6 +70,30 @@ public class StringUtil
return sb.toString();
}
/**
* Pads the supplied string to the requested string width by appending
* spaces to the end of the returned string. If the original string is
* wider than the requested width, it is returned unmodified.
*/
public static String pad (String value, int width)
{
// sanity check
if (width <= 0) {
String errmsg = "Pad width must be greater than zero.";
throw new IllegalArgumentException(errmsg);
} else if (value.length() >= width) {
return value;
} else {
StringBuffer buf = new StringBuffer(value);
while (buf.length() < width) {
buf.append(" ");
}
return buf.toString();
}
}
/**
* Converts the supplied object to a string. Normally this is
* accomplished via the object's built in <code>toString()</code>