If only the end )at the end of the line should be removed, then this works:
str.replaceFirst("\\)$", "");
, : ) (, ), $, , ).
, , ) ( ), , .
), , .*:
str.replaceFirst("(.*)\\)", "$1");
.*, \1. - , \1 , , , ), , ( , right, \1 , ).
O(1) $. O(N), , . , , , O(1) . , .
O(N) - . ) lastIndexOf, O(N).
, Pattern replaceFirst. API:
str.replaceFirst(regex, repl)
,
Pattern.compile(regex).matcher(str).replaceFirst(repl)
" replaceFirst, , ."
, replaceAll , ! , replaceAll replaceFirst , !
needle$ , (.*)needle - , , . "".
replaceFirst - , : , ; , , , "Sense" "Mohican"!
: "spam" . , replace
str.replace("spam", "");
" ! replace ! - ! delete - !"
, ! - - ! - , - replace -ment!
, replaceFirst : -, !
, , , :
public static String removeLastCloseParenthesis(String str) {
return str.replaceFirst("(.*)\\)", "$1");
}
. . , , , .