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

  • SEARCH
  • Home
  • 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 8333463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:02:34+00:00 2026-06-09T03:02:34+00:00

I have two strings plainText and key . Now, if the plainText.length > key.length

  • 0

I have two strings plainText and key. Now, if the plainText.length > key.length then i have to repeat the key until its the same length as that of plainText. here’s an example:

plainText = "helloworld"
key="foobar"

hence the key should be increased to “foobarfoob”.

One solution is to repeat the entire word and then remove the last characters until it reaches the same length as of plainText like “foobarfoobar” and then remove the characters “ar” .

Is there any better (or rather and Elegant) way to do this in java?

  • 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-06-09T03:02:35+00:00Added an answer on June 9, 2026 at 3:02 am

    Your solution seems reasonable, other than the way you remove the last characters – don’t do it one at a time, just use substring or StringBuilder.setLength():

    // Make sure we never have to expand...
    StringBuilder builder = new StringBuilder(plainText.length() + key.length() - 1);
    while (builder.length() < plainText.length()) {
        builder.append(key);
    }
    builder.setLength(plainText.length());
    String result = builder.toString();
    

    Or just change the append call to only get to the right size:

    StringBuilder builder = new StringBuilder(plainText.length());
    while (builder.length() < plainText.length()) {
        builder.append(key.substring(0, Math.min(key.length(),
                                            builder.length() - plainText.length()));
    }
    String result = builder.toString();
    

    Personally I prefer the first – it’s simpler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two strings, one a key and one a value, which I would
If I have two strings that are identical in value, is it guaranteed that
Let's say I have two strings like this: XABY XBAY A simple regex that
Lets say that I have two strings: The System is in halt state. The
Let's say I have two same strings inside an ArrayList... is there a way
I have two strings: prettyCoolString CoolString I want to only get the part that
I have two strings from an android application in ISO 8601 format that I
I have two strings with seemingly the same values. One is stored as a
I have two strings. string a=50; string b=60; Now I want to comapre the
I have two strings: var Number1 = 1CDDE; var Number2 = 01CDDE; The length

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.