Ben
Ben

Reputation: 113

Remove trailing whitespace and terminating character

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

Answers (1)

anubhava
anubhava

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.

RegEx Demo

Upvotes: 1

Related Questions