【问题标题】:Get the string before the certain character in python [duplicate]获取python中某个字符之前的字符串[重复]
【发布时间】:2021-01-09 18:00:45
【问题描述】:

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

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

我的输出应该是'abc eff 23 aaa'

我如何得到它..请帮我解决这个问题

【问题讨论】:

  • 我看到您现在已经发布了几个问题,您应该习惯于在发布新问题之前进行研究。这是来自 Bozho 的blog post,关于如何从您的代码中调试问题,请注意提问是最后步骤之一...
  • 是的,我做到了..但之前找不到相关的帖子..我会浏览那个“博客文章”。谢谢@RichieV
  • @Jung-suk - 作为记录,我通过将您的问题标题复制到谷歌中找到了重复的问题。

标签: python


【解决方案1】:
'abc eff 23 aaa|C:\\WINDOWS|\\Device\\Harddisk\\Parti'.split('|')[0]

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2013-02-07
        • 1970-01-01
        • 2018-08-21
        • 1970-01-01
        • 2019-04-19
        • 2022-11-23
        • 1970-01-01
        • 2017-07-31
        • 1970-01-01
        相关资源
        最近更新 更多