• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

String input with parenthesis

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i want to replace str with pipe symbol.

String str = "(test)";
str=str.replaceAll(str);
System.out.println("output => "+str);

solution:
output => (|)

but i want solution as

output =>
 
Sheriff
Posts: 67756
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please take the time to choose the correct forum for your posts. This forum is for questions on JSP.

For more information, please read this.

This post has been moved to a more appropriate forum.
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you been through the String class, particularly what they call the parameters to the replaceAll method, and what they are described as? Have you been through the links given in the method description, and I trust you are familiar with what they mean? If not, read the Java Tutorials section.

And remember that | is a meta-character, but you might not have that problem if you use | in the right-hand parameter.

*****************************************************************************

I have tried it, and it is easier than I thought. As long as I have understood your request properly!
 
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
...

And remember that | is a meta-character, but you might not have that problem if you use | in the right-hand parameter.
...



Correct: the 2nd parameter is not a regex-aware String. But the $ followed by a number is used to interpolate a matched group from the first parameter (the regex aware String). So, only the '$' needs escaping in the 2nd parameter.
 
reply
    Bookmark Topic Watch Topic
  • New Topic