padding « string « Java Data Type Q&A

1. How can I pad a String in Java?

Is there some easy way to pad Strings in Java? Seems like something that should be in some StringUtil-like API, but I can't find anything that does this.

2. Left padding a String with Zeros

I've seen similar questions here and here. But am not getting how to left pad a String with Zero. input: "129018" output: "0000129018" The total output length should be TEN.

3. Insert padding whitespace into string

Pretty basic problem, but difficult to get into an acceptable form: I want to transform a string by inserting a padding every 3 whitespaces like "123456789" -> "123 456 789" "abcdefgh" -> "abc def ...

4. padding strings - left and right

In the xbase languages there are a number of string functions that I used all the time - padl (pad left) - padr (pad right) and alltrim() - additionally - padl and padr would take two or three parameters - the string to be padded, the length of the new string and optionally the pad character (default to spaces) do these ...

5. Padding Strings

That's about as simple as you can make it in terms of coder efficiency - it's simple to write and hard to screw up. Since this is posted in Beginner, most readers should just stop there and ignore the rest of this post. But since you asked, I'll continue my answer anyway. From a performance perspective, I think it can be ...

6. Padding strings to valid length

Jeff/chi, Thanks for the help. Now that I think about it, a string BUFFER sounds like what I should be using ANYWAY. In COBOL (forgive me) a buffer implied I/O, which is what I'm after. Of course, objects hadn't been invented yet. We thought object was just unreadable code. HA!HA! Thanks again, -Bill.

7. Padding a string to 5 digits

"Cheaper" in what sense? Performance, I presume. However I think it's pretty rare that a method like this is a significant bottleneck. Particularly since formatting a string is typically a prelude to writing it somewhere, and most IO operations will probably take significantly longer than the formatting anyway. I think the main thing to look for here is something that's easy ...

8. Padding a String

I know about the trim string method which will remove trailing spaces from a string. What I want to know is there an easy way to do the reverse, that is add spaces to the end of a string so that it is a minimum length? The best I've been able to come up with is the following for loop. for ...

9. Padding a String

If you're going to do a lot of this you might want to make (or find) a String utility library. You could reuse a method like this over and over: public static String padRight( String input, int length, String pad ) { output = input while output length < desired length output += pad return output substring 0, length } Note ...

10. Truncating/ Padding a string

Hey, I'm having some trouble creating a method (in the form..... public static String padString ( String s, int width, char alignment )) that positions string s in a field of a desired width. If the string is longer than the desired field width, it will need to be truncated. If it is shorter than the desired field width it will ...

11. Padding a String

12. Left Padding a String with ZERO

13. Padding string with zeroes

14. Padding a String

15. Urgent, please help! Padding a String

What is the one way it's different from the other one? It's different in that it uses a value that can change in the stop. Given length of 2. First iteration i=8, length = 2 Second iteration i=7, length =3 Third iteration i=6, length =4 Fourth iteration i=5, length =5 And the loop exits. So you only saw 7 and 8 ...