Reputation: 1609
I have a string in Java, it starts with slashes, but the number of slashes is unknown. How can I remove all theses slashes at start?
For example:
String a="//abc";
Should output abc
.
String b="////abc";
Should also output abc
.
Upvotes: 0
Views: 2309
Reputation: 51445
The trick is to remove the leading slashes, not all slashes.
public class StringTest {
public static void main(String[] args) {
String url = "////abc//def";
System.out.println(url.replaceFirst("/+", ""));
}
}
Upvotes: 2
Reputation: 121710
You can use a simple pattern for that:
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("");
}
Upvotes: 2
Reputation: 2316
public static void main(String[] args) {
String slashString = "//ab/c";
System.out.println(slashString.replaceAll("^/+", ""));
}
output is:
ab/c
Upvotes: 1