• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 976
23
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1845
10
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd make one small stylistic change to Les Morgan's code:

but either is fine.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding to Stefan's solution:


 
Marshal
Posts: 74025
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice solution (apart from the poorly chosen variable names).
 
salvin francis
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Nice solution (apart from the poorly chosen variable names).



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

 
Campbell Ritchie
Marshal
Posts: 74025
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you found it there, it must be wrong
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic