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 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
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: 63
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: 12781
    
    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: 63
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: 172
    
    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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can you write a function that will replace all tokens delimited by @ with a given String?