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 To replace square brackets from a 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 "To replace square brackets from a string" Watch "To replace square brackets from a string" New topic
Author

To replace square brackets from a string

Abhinand Kunjayyappan
Greenhorn

Joined: Mar 28, 2007
Posts: 6
Hi,
Please let me know how to replace [ and ] from a string.

code :
public class Test{
public static void main(String[] args) {
String s = "a\n\"a\"\ta\n [q]\\q$\tq";
System.out.println(s);
s = s.replaceAll("[\\n\\t\\\\\"$/]*"," ");
System.out.println(s);
}
}

In this code i want to replace [ and ] from string s with space.
What should i add to the regular expression to do this.

Thanks Abhinand
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Not an Advanced question at all. Please CarefullyChooseOneForum.

If you want to include square brackets within a list of characters [abc...] in a regular expression, I think you escape them with backslashes. Unfortunately, you then have to escape the backslashes.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Abhinand Kunjayyappan
Greenhorn

Joined: Mar 28, 2007
Posts: 6
Hi,
i changed my codeto replace [ and ].Now it is working

code:

public class Test{
public static void main(String[] args) {
String s = "a\n\"a\"\ta\n [q]\\q$\tq";
System.out.println(s);
s = s.replaceAll("[\\n\\t\\\\\"$/]*"," ").replace('[',' ').replace(']',' ');
System.out.println(s);
}
}

Thanks
Abhinand
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To replace square brackets from a string
 
Similar Threads
Thread
String == with a twist...
character of object array...scjp
Why does this recursion try each permutation?
Explain me this output (notify)