Splitting on first occurrence
在第一次出现分隔符时拆分字符串的最佳方法是什么?
例如:
1 | "123mango abcd mango kiwi peach" |
在第一个
1 | "abcd mango kiwi peach" |
来自文档:
str.split([sep[, maxsplit]]) Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most
maxsplit+1 elements).
1 | s.split('mango', 1)[1] |
1 2 3 4 5 | >>> s ="123mango abcd mango kiwi peach" >>> s.split("mango", 1) ['123', ' abcd mango kiwi peach'] >>> s.split("mango", 1)[1] ' abcd mango kiwi peach' |
对我来说,更好的方法是:
1 | s.split('mango', 1)[-1] |
…因为如果发生这种情况,字符串中就不会出现"EDOCX1"(0)。
因此,