How do you replace a char to another index on the same string? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How do you replace a char to another index on the same string?

for eg i have String="ori",and i want to print out "rio" removing the first char at index zero and puttin it to index two

17th Oct 2021, 1:50 PM
Rio
4 Answers
+ 2
Does it always shift first character to the right? The easiest way would probably be to create a new string, copy one char from original string, then copy substring from original string beginning at first character, up to the second last character. Use of an array of character as temporary buffer could be an option too. There are numerous ways to approach this, including use of built-in data structures e.g. stack.
17th Oct 2021, 1:59 PM
Ipang
+ 2
Ipang and [BusyTheseDays] thankyou 👌
19th Oct 2021, 10:24 AM
Rio
+ 1
Ipang can u show me with an example?
17th Oct 2021, 2:39 PM
Rio
+ 1
This one does the trick by employing the `.substring()` method of `String` class https://code.sololearn.com/c2y5da4h94c8/?ref=app
17th Oct 2021, 3:46 PM
Ipang