• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to change symbols in String to * using regular expressions and replace method?  RSS feed

 
Sergey Lotvin
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Please, help! I have to change symbols in string to *. The quantity of * must be equal to quantity of symbols.
Code:


And it won't work. I get sss that the same to str_rand, but I need for ex.:
str_rand= 23 than I expect sss=**
or
str_rand= 2543 than I expect sss=****
or
str_rand= 3 than I expect sss=*

etc.

What shall I do?
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not do a replace much, but looking in the API for String I see that replaceAll takes a regular expression, but replace does not mention regular expressions.
 
Stefan Evans
Bartender
Posts: 1836
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At the risk of over simplifying, rather than replacing the current string characters, why not just create a string of '*' characters, the same length as the current one?

A simple loop using a StringBuilder to create the string of asterisks would do the trick.


 
Knute Snortum
Sheriff
Posts: 4087
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd make one small stylistic change to Les Morgan's code:

but either is fine.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:At the risk of over simplifying, rather than replacing the current string characters, why not just create a string of '*' characters, the same length as the current one?

A simple loop using a StringBuilder to create the string of asterisks would do the trick.




Stefan your solution is very simple and easy.
 
salvin francis
Bartender
Posts: 1612
36
Eclipse IDE Google Web Toolkit Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adding to Stefan's solution:


 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice solution (apart from the poorly chosen variable names).
 
salvin francis
Bartender
Posts: 1612
36
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Nice solution (apart from the poorly chosen variable names).


Solution was inspired from :
http://www.coderanch.com/forums/posts/list/656672#3041793

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you found it there, it must be wrong
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Adding to Stefan's solution:

And maybe even more practical:Then you can always create a String from the result.

But kudos to both of you.

Winston
 
salvin francis
Bartender
Posts: 1612
36
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you found it there, it must be wrong

I discovered the fill method there a while back when reading that post, liked it.. and when I saw this post, it just came to my mind.
 
salvin francis
Bartender
Posts: 1612
36
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
salvin francis wrote:Adding to Stefan's solution:

And maybe even more practical:Then you can always create a String from the result.

But kudos to both of you.

Winston

Maybe you meant :

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:Maybe you meant...

Doh-h-h! Of course I did. That'll teach me to cut and paste.

Well spotted.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!