aspose file tools*
The moose likes Beginning Java and the fly likes How to append to start of String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to append to start of String?" Watch "How to append to start of String?" New topic
Author

How to append to start of String?

dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi all,


i am having a doubt on how can i append
to the start of string,i tried few methods myself

of StringBuffer like append() but still
it gets appended to the end position
i tried Pattern and Matcher classes (m.start() for start position of string)
as well,but could'nt get the clue onto how to go ahead with appending
string to start position.

but i want to append it to the start position
For ex like String replaced="Placement Date"
i am trying to append "]" to the string
but after i append it shows me
result as "Placement Date]"
i wanted that string should
be "[Placement date]"

so any help and any suggestion onto which method
i must use will be realy great

Thanks in advance
Dhwani:>Winning is not important but it is the only thing.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11231
    
  16

Strings are immutable - once you create one, you can't change it. Now, that's not to say you can't create a new one like you want.

The simplest way would be to just 'add' them together:



If this is all you're doing, and you're not doing it a lot, it will work fine. You just need to be aware that there is some heavy overhead here, and doing it this way thousands or millions of times will cause SERIOUS performance/memory issues.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
And if you do need to use StringBuffer (or even better StringBuilder) to improve performance, take a look at the insert method.


Joanne
dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
Hi All


Thanks a lot to fred rosenberger ,your post cleared
my lots of concepts on String,



Thanks a lot to Joanne Neal i used StringBuffer so
as to improve performance.


Things are working now,

Dhwani:>Winning is not important but it is the only thing.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to append to start of String?