File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to concatenate specified number of characters to a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to concatenate specified number of characters to a string?" Watch "How to concatenate specified number of characters to a string?" New topic
Author

How to concatenate specified number of characters to a string?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 779

Hi all, I want to know how to concatenate specific number of characters to a string. I want my string to have exactly 35 characters. Suppose if the user enter 6 characters, rest of the 29 characters must be any special symbol, say a $.

Using for loop I am append the character by knowing the string length. But is there any efficient method?

Thank you all in advance.


Love all, trust a few, do wrong to none.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 779

Any ideas please.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9939
    
    6

What have you tried yourself? We don't hand out ready-made solutions or provide answers to what appear to be homework problems.

show us what you've done, and we can guide you.


Never ascribe to malice that which can be adequately explained by stupidity.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1046

Check out already existing methods of String.


SCJP6.0,My blog Ranchers from Delhi
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 779

fred rosenberger wrote:What have you tried yourself? We don't hand out ready-made solutions or provide answers to what appear to be homework problems.

show us what you've done, and we can guide you.


I asked for an idea dude. I am using this code right now
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Instead of using String you could use StringBuilder and append() method. That's a bit more eficient.

Instead of using a loop and appending one char at the end, you could use 'coin method'. Having static final strings of length 20, 10, 5, 2 and 1 characters each. Append those strings, depending on the space you need to fill. You would probably still use a loop, it would just take less cycles.


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 779

Thanks Martin.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9939
    
    6

Concatenate your input string with a string 35 of your 'special characters'
return a substring of that, from 0-34
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
fred rosenberger wrote:Concatenate your input string with a string 35 of your 'special characters'
return a substring of that, from 0-34


Brilliant!
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 779

Thanks Fred.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

Nice solution Fred

but I was wondering if String.format() will help in any way here ?


Time is what we want the most, but what we use the worst. -- William Penn
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to concatenate specified number of characters to a string?
 
Similar Threads
Limit JTextField number of characters
How to display all of the ascii characters
Need some help regarding arraylist
Check the Characters in Text Box!
String concatenation