如何在特定子字符串之后获取字符串? [英] How to get a string after a specific substring?

查看:47
本文介绍了如何在特定子字符串之后获取字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在特定子字符串后获取字符串?

How can I get a string after a specific substring?

例如,我想在my_string=hello python world"中获取"world"之后的字符串,我是初学者"(在这种情况是:,我是初学者)

For example, I want to get the string after "world" in my_string="hello python world , I'm a beginner " (which in this case it is:, I'm a beginner)

推荐答案

最简单的方法可能就是拆分目标词

The easiest way is probably just to split on your target word

my_string="hello python world , i'm a beginner "
print my_string.split("world",1)[1] 

split 接受要拆分的单词(或字符)和可选的拆分次数限制.

split takes the word(or character) to split on and optionally a limit to the number of splits.

在这个例子中,在world"上拆分,并将其限制为一个拆分.

In this example split on "world" and limit it to only one split.

这篇关于如何在特定子字符串之后获取字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆