Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adding a Substring to a String without knowing the position

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,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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]]

>Solution :

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]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading