6

I would like to take the 's' out of http.

 https://joy.tothewor.ld/today/and/tommorrow

 http://joy.tothewor.ld/today/and/tommorrow

What's the fastest/less expensive way?

substring, string builder, something newer in Android's SDK?

1
  • The least expensive is if(string.startsWith("https:")) string = "http"+string.substring(5); Commented Feb 12, 2018 at 15:21

3 Answers 3

21

String.replaceFirst will do the job.

String output = input.replaceFirst("s","");
Sign up to request clarification or add additional context in comments.

Comments

4

String.replaceFirst is heavyweight

 public String replaceFirst(String regex, String replacement) {
        return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
 }

this is the fastest way

str = str.substring(0, 4) + s.substring(5);

3 Comments

public String replaceSS(String s) { String xincomingScene; xincomingScene = xincomingScene.substring(s, 4) + s.substring(4); return xincomingScene; }
the method substring(int,int) in the Type String is not applicable for the arguments(String, int)
That’s of course only correct, if you know that the intended character is at the index 4. But if you know beforehand that str starts with "https" and is supposed to be changed to "http…", it might be even faster to say str = "http" + s.substring(5);
1

You can try this

String str="https://joy.tothewor.ld/today/and/tommorrow"
                                            .replace("https://","http://");

2 Comments

that is also a good one .. actually there are plenty of options .. :) .. +1
but whats the least expensive

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.