From 46b8947354acd4a315bb596a6017dd28cd3f2f45 Mon Sep 17 00:00:00 2001 From: mdb Date: Sat, 23 Feb 2002 18:24:02 +0000 Subject: [PATCH] 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 --- .../java/com/samskivert/util/StringUtil.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java index 679bf531..19a6d3f7 100644 --- a/projects/samskivert/src/java/com/samskivert/util/StringUtil.java +++ b/projects/samskivert/src/java/com/samskivert/util/StringUtil.java @@ -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 toString()