This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Can you write a function that will replace all tokens delimited by @ with a given String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can you write a function that will replace all tokens delimited by @ with a given String?" Watch "Can you write a function that will replace all tokens delimited by @ with a given String?" New topic
Author

Can you write a function that will replace all tokens delimited by @ with a given String?

jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 60
Can you write a function that will replace all tokens delimited by @ with a given String?
Sample Input = “Hello @name@, where are you @name@?”

I am expecting out this way: Hello Jami, where are you Jami?

But i am getting this way "HelloJamiwhereareyouJami" when I tried with below logic. Can any One would give better example will be helpful.

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12756
    
    5
You have to put in the spaces yourself, the tokenizer discards them of course.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
But there is a StringTokenizer constructor that allows you to keep them.
jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 60
The Issue is not with only spaces it removes , and ? this also.

That is why i am expecting different implementation.
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 169
    
    1

You can do this simply by replace() method in String class.

From the constructor you used, tokens are created by splitting your string from whitespaces. Therefore whitespaces are omitted. I don't think putting spaces yourself is not a good answer. Here, your 'comma' is also omitted.
I think you have to use StringTokenizer(String str, String delim) constructor. Then you have to change your codes completely..! (Refer java API docs for help)


Ramesh-X
 
Don't get me started about those stupid light bulbs.
 
subject: Can you write a function that will replace all tokens delimited by @ with a given String?
 
Similar Threads
Why iam getting ClassCastException
StringBuff/Vector perf on Solaris
next line in swings
Hibernate Search indexing...w/ Compass
Static fields are not populated