Adding a Substring to a String without knowing the position

Question:

I want to add a char(newline) to a String by specifiying the location with a Substring.

Is that possible in Python or Java?

Here’s how I imagine it,

Newline is added to the String at the position between two arrays '],':

str = [[a,b,c], [d,e,f]]

result = addString(str, '],', 'n')
print(result)

Output:

[[a,b,c],
[d,e,f]]
Asked By: vrubayka

||

Answers:

Python-wise, your str variable uses a reserved keyword and missing the quotes.

you can do it in python as below:

mystr = "[[a,b,c], [d,e,f]]"       
print(mystr.replace('],', '],n'))

prints:

[[a,b,c],
 [d,e,f]]
Answered By: ilias-sp
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.