Reputation: 113
String val = "this is a string *"
I am attempting to remove the final character '*' along with any preceding whitespace with the following output:
String result = "this is a string"
What's the simplest and sturdiest approach to doing this?
Should I substring the final character and trim the string or use regex?
Upvotes: 1
Views: 1984
Reputation: 784968
For matching you can use:
\s*\*$
Which is 0 or more whitespace characters followed by literal * character with anchor $
to assert end of line.
That needs to be replaced by empty string ""
to remove it.
Upvotes: 1