Results 1 to 3 of 3
- 09-02-2009, 05:03 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 7
- Rep Power
- 0
Get item from string array and pack with blank space
here is the string array value:
String[] regionList
where
regionList[0] = "AAA"
regionList[1] = "BBBB"
I would like to check all items and change to fix length 4
that is
regionList[0] = "AAA "
regionList[1] = "BBBB"
How could I get from the string array and add " " to the end of the item?
:):):):):):)
- 09-02-2009, 06:07 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
You get the strings from the array using the index notation as you did in your post. Ie
regionList[0] is the first string in the array
regionList[1] is the second.
Since Strings are immutable you will have to replace the string in the array with the padded string.
You could concatenate the string with a space:
Or you could use StringBuilder which offers an append() method. The most flexible (but complicated) method would be to use the Formatter class.Java Code:regionList[0] = regionList[0] + " ";
The format expression "%-4s" means "format to width 4 padding with spaces on the right" as described in the API documentation linked to above.Java Code:StringBuilder buf = new StringBuilder(); Formatter formatter = new Formatter(buf); regionList[0] = formatter.format("%-4s", regionList[0]).toString(); System.out.printf("|%s|%n", regionList[0]);
- 09-02-2009, 07:38 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 7
- Rep Power
- 0
Similar Threads
-
Extract item from a string
By firewalll in forum New To JavaReplies: 2Last Post: 09-02-2009, 05:00 AM -
Blank space
By sandy1028 in forum New To JavaReplies: 1Last Post: 04-21-2009, 10:00 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM -
Print a blank space
By susan in forum New To JavaReplies: 2Last Post: 07-30-2007, 01:58 PM -
visual web pack
By Jack in forum NetBeansReplies: 2Last Post: 07-02-2007, 05:13 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks