Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to append to start of String?

 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if you do need to use StringBuffer (or even better StringBuilder) to improve performance, take a look at the insert method.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic