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

gazeldx · March 04, 2022 · Last by pynix replied at March 04, 2022 · 209 hits

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(). 😀

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

You need to Sign in before reply, if you don't have an account, please Sign up first.