【问题标题】:How to remove character after specific character?如何在特定字符后删除字符?
【发布时间】:2012-09-15 12:30:47
【问题描述】:

我有例如这个字符串:

hello.name-2.txt

我只需要删除“-”之后的字符。

所以我的输出应该是这样的:

hello.name-.txt

我该怎么做?

【问题讨论】:

    标签: java string character


    【解决方案1】:

    你可以的

    s = s.replaceAll("-.", "-");
    

    如果你想替换一个数字,甚至“hello.name-1234.txt”你可以使用

    s = s.replaceAll("-\\d+", "-");
    

    如果您只想执行一次,可以改用replaceFirst

    【讨论】:

    • 很好,我会在五分钟内接受你的回答,我现在接受不了;)
    【解决方案2】:
     int dashIndex = yourString.indexOf("-");
     String result = yourString.substring(0, dashIndex + 1) 
                   + yourString.substring(dashIndex + 2);
    

    【讨论】:

      猜你喜欢
      • 2016-05-11
      • 1970-01-01
      • 2022-10-12
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多