简体   繁体   中英

How to concatenate strings in Java?

The strKeyword will be repeated according to the loop. How can I Save the outcome as a new string. For example if the work "hello" was repeated twice, how would I now create the "hellohello" as a completely new string.

for (int l = 0; l < newKeywordLength; l++) {          
    System.out.print(strKeyword);
}

just use

strKeyword+=strKeyword;

since java 6 well optimized, no need for a stringbuilder

string newWord="";
for (int l=0; l<newKeywordLength; l++){           
         newWord+=strKeyword;
}
System.out.print(newWord);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM