Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
jami siva
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13055
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to put in the spaces yourself, the tokenizer discards them of course.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But there is a StringTokenizer constructor that allows you to keep them.
 
jami siva
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic