1

I wish to purge the http:// and www. parts of an URL String using one statement.

I am not after wizardly regex-solutions, I simply want to know if there is a way to replace (read: remove) both words in one single replace statement.

Dream scenario:

String url = "http://www.superfect.com";
String[] purge = {"http://", "www."};
url = url.replace(purge, "");

This does not run, however. How is this usually done in Java?

2
  • Why does it need to be in a single statement? Commented Jan 29, 2014 at 23:39
  • It doesn't need to, I just expected java to be modern enough to be able to process an array rather than looping/doing multiline replacements. String replacement and substitution is quite a common task, so. Commented Jan 29, 2014 at 23:45

6 Answers 6

7

In a single line, with a single replacement action:

url = url.replaceAll("http://|www\\.", "");
Sign up to request clarification or add additional context in comments.

Comments

2

Do this in a simple loop:

String purge[] = {"www.", "http://", "https://", "ftp://"};
String result = url;
for (int i = 0; i < purge.length; ++i)
{
    result = result.replace(purge[i], "");
}

Now, the result String is the one you want. As codesalsa pointed out and given the context of URL's, you might want to do it this way:

String purge[] = {"http://", "https://", "ftp://", "www."}; //order is important!
String result = url;
for (int i = 0; i < purge.length; ++i)
{
    if (result.startsWith(purge[i])
    {
        result = result.substring(purge[i].length);
    }
}

2 Comments

You should add a startsWith check first... what if superfectwww.com.
@codesalsa: You are right. Initially, I didn't care about the exact situation. I just provided a way to batch replace all elements of an array. However, I added some nice code :).
1

Java doesn't offer a method for replacing more than one literal character sequence at a time. Regular expressions could be used, to match both intended replacement targets in the same call.

Without regular expressions, you need to call replace once for each target.

url = url.replace("http://", "").replace("www.", "");

3 Comments

+1 for regular expression. I would suggest adding a code fragment to show the replaceAll() approach.
@JustinKSU I would have, except the OP stated "I am not after wizardly regex-solutions".
And right you are in that. Too many things can be hacked together with an sufficiently funky regex, I just wanted to know what was available "out of the box".
1

You could do

url = url.replaceAll("http://(?:www\\.)?", "");

Comments

1

You've got already plenty of nice & working solutions written right here. I'm, however, kinda a fan of clean, easily-understandable code. There's nothing more wonderful than elegant one-command solution. Here you go. You're welcome!

public class Test {
        public static String parseDomainName(String url) {
            return (
                url.startsWith("http://www.") ? url.replaceFirst("http://www\\.", "") :
                    url.startsWith("http://") ? url.replaceFirst("http://", "") :
                        url.startsWith("www.") ? url.replaceFirst("www\\.", "") :
                            url
            );
    }

    public static void main(String[] args) {
        System.out.println(parseDomainName("http://www.google.com"));
        System.out.println(parseDomainName("http://google.com"));
        System.out.println(parseDomainName("www.google.com"));
        System.out.println(parseDomainName("google.com"));
        System.out.println(parseDomainName("http://misleading.www.com"));
    }
}

Alright, I'm just joking! But it's the single solution right here, which works in one command and doesn't use regular expressions (well, method replaceFirst() actually accepts only a regular expressions, but it'd be working on the same logic even with different method, which accepts only plain text string).

Use this solution as it's the best compromise if you really want to avoid using regular expressions. This solution I've made is really just a joke and it'd be horrible to see it used somewhere. :-)

Comments

0

How about this?

url = url.replace("http://www.", "");

Wouldn't that work?

3 Comments

It would, but in the list of URL's there are occurrences where the www-part is excluded, so I need to remove them separately.
OK. Why do you need exactly one statement?
I don't need to! Doing multiple, singular replacements on separate lines doesn't feel as lean as replacing by an Array.

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.