• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to append to start of String?

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic