Resize string / Call stated part of the string

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dmitriy Romanov

I am trying to get better with recursion, and one excercise I came upon was to revert a string. At first I was trying to solve this by any means possible, and my first code had ~50 lines. Step by step, improving it , and reading all the Google, I ended up with this:

func three(input):
if input == "":
	return input
	return input[-1] + three(input.resize(len(input) - 1))

The only problem is - there’s no resize function with strings. Alternatively, I’ve seen solutions doing “return everything but the last symbol”.
Here’s the best explanation: Python reversing a string using recursion - Stack Overflow

This is guy’s code:

def backward(text):
if text == "":
    return text
    return text[-1] + backward(text[:-1])

(Resembles something, huh?))
But. There’s no “call everything but the last symbol” function/syntaxis in GDSript.

Or is there? Please, help.

:bust_in_silhouette: Reply From: duke_meister

I don’t see why you need to resize the string. It’s going to end up the same size isn’t it?

Anyway, you want to usesubstr ( int from, int len ) and use 0 as from, and input len-1 as len, instead of input.resize(len(input) - 1)

Yeah, I’m blind. Was reading documentation top to bottom and bottom to top, but did not see that function. Thanks.

Dmitriy Romanov | 2018-07-11 09:47