Why it doesn't work?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By danilo00782
var q = ["Test1","Test2"]
q[0].erase(0,3)
print(q[0])

Output: Test1

:bust_in_silhouette: Reply From: Thomas Karcher

q[0].erase(0,3) gets the value of q[0], not the reference to it, so doing anything with this value will not affect the value stored in the array itself.

This should work:

temp_string = q[0]
temp_string.erase(0,3)
q[0] = temp_string

(Replaces the array element with the changed string)

Or even simpler:

q[0] = q[0].substr(4)

(Does everything in one step, but needs a method with a return value)