+ 1

How can I concatenated two strings alternatively ?

S1="ab",S2="xy", concatenated="axby" . Just tell me the how to code and the logic explanation also ? I'll be thankful to him/her .☺️

4th Jul 2019, 8:47 AM
Animesh Banerjee
Animesh Banerjee - avatar
7 ответов
+ 5
String s3= s1+s2; or String s3= s1.concat(s2); https://www.sololearn.com/learn/Java/2142/
4th Jul 2019, 8:50 AM
voja
voja - avatar
4th Jul 2019, 9:00 AM
voja
voja - avatar
4th Jul 2019, 9:46 AM
Sarthak Pokhrel
Sarthak Pokhrel - avatar
+ 1
In that way I got abxy but I need axby !!
4th Jul 2019, 8:52 AM
Animesh Banerjee
Animesh Banerjee - avatar
+ 1
it is not about concatenate, but about how divide this strings String s1="ab", s2="xy"; System.out.println( ""+s1.charAt(0) +s2.charAt(0) +s1.charAt(1) +s2.charAt(1) ); you can use eg s1.substring(0,1) instead
4th Jul 2019, 9:33 AM
zemiak
+ 1
StringBuilder builder = new StringBuilder(s1.length() + s2.length()); for(int i = 0; i < s1.length(); i++) { builder.append(s1.charAt(i)); builder.append(s2.charAt(i)); } String s3 = builder.toString(); For each valid indexes of your strings, append the characters at this index from both strings, to an accumulator.
4th Jul 2019, 10:06 AM
kumesana
0
No sir
4th Jul 2019, 9:01 AM
Animesh Banerjee
Animesh Banerjee - avatar