0

Can you remove leading-and-lagging characters-and-substrings from a string without using RegEx-and/or-Custom-Code?

For example, I want to remove leading https:// and lagging / (backslashes).

I can definitely solve this with custom code and/or RegEx. But we don't want to go ahead with these answers after discussion. Many people are uncertain about this change and do not want to spend time learning RegEx to verify. We just want to make it easy and simple to switch-out and verify is working after refactoring. Is there there a package solution or built-in that already handles find-replace first-and-last?

@Value("${elsewhere.host.url:https://url.or.uri.full.or.partial}")
String urlOrUri;

// ...

String scheme = "https://"

// Example 1: 
if (urlOrUri.startsWith(scheme))
    urlOrUri.replaceFirst(scheme,"");
if (urlOrUri.startsWith("/"))
    urlOrUri.replaceFirst("/","");
if (urlOrUri.endsWith("/"))
    urlOrUri.replaceLast("/","");

// Example 2:
urlOrUri.replaceFirst("^https://+", "");
urlOrUri.replaceFirst("^/+", "");
urlOrUri.replaceFirst("/+$", "");

https://regex101.com/

1
  • 1
    Your code to remove a trailing / won't work unless there is only that / in the url... Commented Apr 17, 2024 at 7:39

1 Answer 1

0

What's wrong with "custom code"? Everything you write is custom code.

Instead of repeating

if (urlOrUri.startsWith(scheme))
    urlOrUri.replaceFirst(scheme,"");

write a method. And if you use .substring instead of replaceFirst there won't be any "rematching":

public String removePrefix(String input, String prefix) {
  if (input.startsWith(prefix)) {
     return input.substring(prefix.length());
  }
  return input;
}

Also the there is not much to learn about regexps the way you are using them here, except the regexps you are using don't do the same as example 1:

removePrefix("//example", "/"); // Returns "/example"
"//example".replace("^/+", ""); // Returns "example"

Even if you use the regexp, you should also write a method, that does this, instead of writing each regexp separately:

public String removePrefix(String input, String prefix) {
  return input.replace("^" + Pattern.quote(prefix), "");
}

One more point: Since you are working with URLs/URIs, maybe you could use a URL parser library, but that depends on why you are doing this and what you actually are thing to achieve.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the heads-up about my quesiton
I agree there's nothing wrong with custom code. But I don't think I have enough persuasion on the decision here.
And yes, this question was initially more focused toward URls. And I have tried looking around for a URL-parser-library. But I haven't found one that handles trimming yet.
Although this question is more aimed at URIs, I would like to know a more general and/or abstracted solution if they exist as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.