aoscici2000
V2EX  ›  Java

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

  •  
  •   aoscici2000 · Oct 17, 2019 · 4597 views
    This topic created in 2596 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
    Telegram
    Oct 17, 2019 via iPhone
    1
    正则表达式
    vakara
    vakara
    Oct 17, 2019 via Android
    2
    '-'.join(s.split('-')[:-1])
    lululau
    lululau
    Oct 17, 2019
    3
    .split("-(?!.*-)")
    Raymon111111
    Raymon111111
    Oct 17, 2019
    4
    找到最后一个 - 的 index

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