Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4563254
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:19:55+00:00 2026-05-21T18:19:55+00:00

I am trying to replace a URL with a shortened URL inside of a

  • 0

I am trying to replace a URL with a shortened URL inside of a String:

public void shortenMessage()
    {
        String body = composeEditText.getText().toString();
        String shortenedBody = new String();
        String [] tokens = body.split("\\s");

        // Attempt to convert each item into an URL.  
        for( String token : tokens ) 
        {
            try
            {
                Url url = as("mycompany", "someapikey").call(shorten(token));
                Log.d("SHORTENED", token + " was shortened!");
                shortenedBody = body.replace(token, url.getShortUrl());
            }
            catch(BitlyException e)
            {
                //Log.d("BitlyException", token + " could not be shortened!");

            }
        }

        composeEditText.setText(shortenedBody);
        // url.getShortUrl() -> http://bit.ly/fB05
    }

After the links are shortened, I want to print the modified string in an EditText. My EditText is not displaying my messages properly.

For example:

"I like www.google.com" should be "I like [some shortened url]" after my code executes.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:19:56+00:00Added an answer on May 21, 2026 at 6:19 pm

    In Java, strings are immutable. String.replace() returns a new string which is the result of the replacement. Thus, when you do shortenedBody = body.replace(token, url.getShortUrl()); in a loop, shortenedBody will hold the result of (only the very) last replace.

    Here’s a fix, using StringBuilder.

    public void shortenMessage()
    {
        String body = composeEditText.getText().toString();
        StringBuilder shortenedBody = new StringBuilder();
        String [] tokens = body.split("\\s");
    
        // Attempt to convert each item into an URL.  
        for( String token : tokens ) 
        {
            try
            {
                Url url = as("mycompany", "someapikey").call(shorten(token));
                Log.d("SHORTENED", token + " was shortened!");
                shortenedBody.append(url.getShortUrl()).append(" ");
            }
            catch(BitlyException e)
            {
                //Log.d("BitlyException", token + " could not be shortened!");
    
            }
        }
    
        composeEditText.setText(shortenedBody.toString());
        // url.getShortUrl() -> http://bit.ly/fB05
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to replace a friendly url pattern with a html url notation but
I am trying to replace $1, $2, $3 variables in a URL with another
I am trying to replace a large string in groovy. But can't get it
I'm trying to replace all link href's in a large string with a different
I am trying to replace relative url from /abc/bbc.do?count=1 with /bbc.do?count=1 using urlrewritefilter but
I'm trying to do a URL GET variable replace, however the regular expression for
I am trying to replace the url values on the entire page using some
I am trying to replace an Image URL which changes constantly. Image URL: src=/images/previews/logo-15-hp.jpg
I am trying to replace my Pagination within OpenCart. From this: $pagination->url = $this->url->link('product/search',
As new to groovy... I'm trying to replace the java idiom for event listeners,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.