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 8472041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:57:31+00:00 2026-06-10T16:57:31+00:00

I am trying to concat String array values in a String, The below code

  • 0

I am trying to concat String array values in a String, The below code does not work

private void newString() {

    String str = "Split me"; 
    String[] tokens = str.split("[ ]+");

    String newStr = new String();

    for(int i=0; i<tokens.length; i++){
        newStr.concat(tokens[i]);
    }

    System.out.println("NEW STRING IS : " + newStr);
}


public static void main(String[] args){
    Main m = new Main();
    m.newString();
}
  • 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-10T16:57:33+00:00Added an answer on June 10, 2026 at 4:57 pm

    String is immutable. Naturally, String.concat returns a brand new String.

    … a new String object is created, representing a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

    The way you’re trying to do it, the code should look like…

    String glued = "";
    
    for (final String token : tokens) {
      glued = glued.concat(token);
    }
    

    Now, the problem I have with this approach is that you’re doing a lot of copies of the String data. Each concat copies all of the data in glued as well as the data in token. Now, think about the implications of that… I’m not a fan of premature optimization, but I believe the logical way to achieve this is to instead use a StringBuilder as follows…

    final StringBuffer buf = new StringBuffer();
    for (final String token : tokens) {
      buf.append(token);
    }
    final String glued = buf.toString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to replace 1234567890123456 with ************3456 with the following code: Regex.Replace(xml,@\b\d{13,16}\b, string.Concat(new
Trying to build out an exception if move.UserId does not equal currentUserId then Redirect
So I have the following code: <redirect:write select=concat('..\\folder\\,string(filename),'.xml')> Where filename is a tag in
I'm trying to pass an array of string parameters to a C# ASP.NET web
I'm trying to remove forbidden chars from a string. $forbidden = array( <, >,
I have parameters in SP and trying to make them a concat to string
What I'm trying to do is as follows: (let ((pj-path (concat src haxe-project-generator))) (when
Trying to copy the msdn refernce here doesn't work and gives an error I
I have two string variables ticker and detail. I'm trying to print out the
I'm not trying to ask you guys to help me to do homework because

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.