简体   繁体   中英

Groovy: remove specific characters from end of string

I have a string that might end with multiple \\n (the two symbols, not a newline).

How can I remove that from the end of the string?

For example: abc\\ndef\\n\\n should become abc\\ndef

Thanks

对于这样一个简单的任务,一个简单的trim()就足够了:

assert 'abc\ndef' == 'abc\ndef\n\n\n\n'.trim()

You can do it like this:

s = "abc\ndef\n\n"
assert s.replaceAll(/\n*$/, "") == "abc\ndef"

The section between // looks for any amount of \\n and the $ represents the end of the string. So, replace any amount of newlines with nothing else after them, with an empty string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM