获取python中特定字符前面的字符串

2024-06-16 12:44:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的字符串。我想在角色之前得到一切

'abc eff 23 aaa|C:\\WINDOWS|\\Device\\Harddisk\\Parti'

我的输出应该是“abc eff 23 aaa”

我怎么得到它。。请帮我做这个


Tags: 字符串角色devicewindowsabceffaaaparti
3条回答

您可以使用内置的“拆分”功能:

'abc eff 23 aaa|C:\\WINDOWS|\\Device\\Harddisk\\Parti'.split('|')[0]

Here is the documentation on how it works if you are curious

这里有两种方法可以使用string.split('|')[0]string[:string.find('|')](字符串是数组,您也可以这样做)

'abc eff 23 aaa|C:\\WINDOWS|\\Device\\Harddisk\\Parti'.split('|')[0]

相关问题 更多 >