[英]How to Remove special Characters from Java String at the end and at the beginning
[英]How to remove the square brackets at the beginning and end of the special characters String?
下面是我想使用 Java 刪除方括號的示例:-
String specialCharacters = "[!& *=~}[]"
String specialCharactes1 = "[[[&%*@#(#][[]"
我分別想要這樣的結果:-
!& *=~}[
[[&%*@#(#][[
這意味着我只想從兩端刪除一個方括號。 我看到了很多解決方案,但這些都與 JavaScript 有關。
這可以使用正則表達式來完成,但我不確定如何復制它。
感謝@Elliott Frisch 的解決方案。 這只是引號的愚蠢錯誤。 你的解決方案奏效了。
System.out.println(specialCharacters.replaceAll("\\[(.+)\\]", "$1"));
System.out.println(specialCharactes1.replaceAll("\\[(.+)\\]", "$1"));
String specialCharacters = "[!& *=~}[]";
if(null!=specialCharacters)
{
String answer = specialCharacters.substring(specialCharacters.indexOf("
[")+1,specialCharacters.indexOf("]"));
}
如果輸入字符串中只有一個 [ 和 ],這將起作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.