If you are displaying the String in a browser, padding the string with
spaces will not produce the desired results since HTML ignores extra spaces
and newline characters. Instead, you need to append " " in place of a
space character.
String padString(String s, int desiredSize)
{
String SPACE = " ";
StringBuffer sb = new StringBuffer();
// Validate the input parameters
if (desiredSize < 0)
desiredSize = 0;
if (s == null || s.equals(""))
{ // if s is null or is an empty string, return all spaces
for(int i=0;i<desiredSize;i++)
sb.append(SPACE);
return sb.toString();
}
// add the original string to the string buffer
sb.append(s);
// append the spaces
for(int i=s.length();i<desiredSize;i++)
sb.append(SPACE);
return sb.toString();
}
-Richard
-----Original Message-----
From: B R Nair [mailto:[EMAIL PROTECTED]]
Sent: Friday, July 28, 2000 6:26 AM
To: [EMAIL PROTECTED]
Subject: Proprtionate String Padding
Hello,
How can I pad a string with variable spaces?
When I query a database, the columns I get are not of fixed length. I would
like to make it uniform length. Is there any short cut to add spaces at the
end of a string? In other words can anyone give me the Java equivalent to
this pseudo code?
desiredString = string1 + (35 - string1.length()) spaces.
Thanks in advance
BRN
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets