本文介绍了在第一次出现时拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第一次出现分隔符时拆分字符串的最佳方法是什么?

What would be the best way to split a string on the first occurrence of a delimiter?

例如:

"123mango abcd mango kiwi peach"

分割第一个 mango 得到:

"abcd mango kiwi peach"

推荐答案

来自 文档:

str.split([sep[, maxsplit]])

返回字符串中单词的列表,使用 sep 作为分隔符字符串.如果给定了 maxsplit,则最多完成 maxsplit 次拆分(因此,列表将最多包含 maxsplit+1 个元素).

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).

s.split('mango', 1)[1]

这篇关于在第一次出现时拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-13 09:22