aoscici2000
V2EX  ›  Java

怎样按照最后一个字符分割字符串

  •  
  •   aoscici2000 · Oct 17, 2019 · 4077 views
    This topic created in 2398 days ago, the information mentioned may be changed or developed.

    例如 12-34-56-78-9

    分成两个"12-34-56-78" 和 "9"

    6 replies    2019-10-19 11:36:21 +08:00
    Telegram
        1
    Telegram  
       Oct 17, 2019 via iPhone
    正则表达式
    vakara
        2
    vakara  
       Oct 17, 2019 via Android
    '-'.join(s.split('-')[:-1])
    lululau
        3
    lululau  
       Oct 17, 2019
    .split("-(?!.*-)")
    Raymon111111
        4
    Raymon111111  
       Oct 17, 2019
    找到最后一个 - 的 index

    然后用这个 index 去分
    ColoThor
        5
    ColoThor  
       Oct 17, 2019
    int index = str.lastIndexOf("-");
    str.substring(0, index);
    str.substring(index + 1);
    aoscici2000
        6
    aoscici2000  
    OP
       Oct 19, 2019
    @ColoThor 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1167 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 18:09 · PVG 02:09 · LAX 11:09 · JFK 14:09
    ♥ Do have faith in what you're doing.