提问人:vrubayka 提问时间:3/5/2023 更新时间:3/5/2023 访问量:55
在不知道位置的情况下将子字符串添加到字符串
Adding a Substring to a String without knowing the position
问:
我想通过使用 Substring 指定位置来向 String 添加 char(newline)。
这在 Python 或 Java 中可能吗?
这是我的想象,
换行符被添加到两个数组之间的字符串中:'],'
str = [[a,b,c], [d,e,f]]
result = addString(str, '],', '\n')
print(result)
Output:
[[a,b,c],
[d,e,f]]
答:
1赞
ilias-sp
3/5/2023
#1
在 Python 方面,您的变量使用保留关键字并缺少引号。str
您可以在 Python 中执行此操作,如下所示:
mystr = "[[a,b,c], [d,e,f]]"
print(mystr.replace('],', '],\n'))
指纹:
[[a,b,c],
[d,e,f]]
评论
2赞
vrubayka
3/5/2023
谢谢,为了达到问题中描述的预期效果,我们可以稍微更改代码:print(mystr.replace('], ', '],\n'))
评论
split()