Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

java - Remove square brackets from a String using Regular Expressions?

How would I remove all square brackets ("[]") from a given String in Java?

String s = "[abcdefg]";
s = s.replaceAll(regex, "");

What regular expression would be used in this case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use this one:

 String s = "[abcdefg]";
 String regex = "\[|\]";
 s = s.replaceAll(regex, "");
 System.out.println(s);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...