如何删除字符串中的所有前导斜杠?

时间:2016-02-18 20:38:41

标签: java

我在Java中有一个字符串,它以斜杠开头,但是斜杠的数量是未知的。如何在开始时删除所有这些斜杠?

例如:

String a="//abc";

应输出abc

String b="////abc";

还应输出abc

3 个答案:

答案 0 :(得分:2)

您可以使用简单的模式:

private static final Pattern HEADING_SLASHES = Pattern.compile("^/+");

// ...

public static String removeHeadingSlashes(final String input)
{
    // .replaceFirst() or .replaceAll() will have the same effect, so...
    return HEADING_SLASHES.matcher(input).replaceFirst("");
}

答案 1 :(得分:2)

诀窍是删除前导斜杠,而不是全斜杠。

public class StringTest {

    public static void main(String[] args) {
        String url = "////abc//def";
        System.out.println(url.replaceFirst("/+", ""));
    }

}

答案 2 :(得分:1)

MakePRI : warning 0xdef01051: No default or neutral resource given for 'Files/Project/Strings/testfile_de.xml'. The application may throw an exception for certain user configurations when retrieving the resources.
MakePRI : warning 0xdef00522: Resources found for language(s) 'en,de' but no resources found for default language(s): 'en-US'. Change the default language or qualify resources with the default language.

输出是:

  

AB / C