分享 Best way for Python and Java to reverse a string

gazeldx · 2022年03月04日 · 最后由 pynix 回复于 2022年03月04日 · 217 次阅读

Python

According to reverse-a-string-in-python, the solution would be

'hello world'[::-1]

To make it more readable, I have to define a function:

def reverse(target_string):
    return target_string[::-1]

Java

According to reverse-a-string-in-java, the solution would be

new StringBuilder('hello world').reverse().toString()

I think for Python, there should be a method called reverse() to let me use it like 'hello world'.reverse(). 😀

虽然希望有一个,但是这个功能感觉用得挺少,字符串倒过来一般情况下会失去意义,不过集合到是经常需要倒过来。

需要 登录 后方可回复, 如果你还没有账号请 注册新账号