how to merge two strings into one by using chartAt() and arrays technique

java string join array
java string join list
string.join java
java string split
string concatenation in java using for loop
java substring
java string join with delimiter
string.join java 8

as mentioned in the title, I got an error in each line inside for loop which says (variable expected) and this is my code

 String s = "ABC";
    String t = "DEFGH";
    String merge = "";
    // merge should looks like "ADBECFGH"
    int i = 0;
    for (; i < s.length(); i=i+2) {
        merge.charAt(i) = s.charAt(i/2);
        merge.charAt(i+1) = t.charAt(i/2);
    for (; i < t.length()+s.length() ; i++) {
        merge.charAt(i) = t.charAt(i-s.length());

am trying to use same technique with arrays which I think its very effective.

Another one, since you are manipulating Strings inside a loop, it is better to use StringBuilder instead of String.

    String s = "ABC";
    String t = "DEFGH";
    StringBuilder merge = new StringBuilder();
    for (int i = 0; i < s.length() || i < t.length(); i++) {
        if (i < s.length()) {
        if (i < t.length()) {


How to concatenate multiple strings (C# Guide), How do you concatenate two strings in Java without using the library function? Merge two sorted arrays with O(1) extra space Merge k sorted arrays | Set 1. This article is contributed by Sahil Chhabra. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

If you like take one letter from first string and then from other try this:

for (int i = 0; i < s.length() || i < t.length(); i++) {
    if (i < s.length()) {
        merge += String.valueOf(s.charAt(i));
    if (i < t.length()) {
        merge += String.valueOf(t.charAt(i));

This is condition that let you iterate till longer String finish

i < s.length() || i < t.length()

Python String Concatenation, charAt(i/2); } for (; i < t.length()+s.length() ; i++) { merge.charAt(i) = t.charAt(i-s.​length()); }. am trying to use same technique with arrays which I  Alternatively Merge two Strings in Java Given 2 strings, merge them in an alternate way, i.e. the final string’s first character is the first character of the first string, the second character of the final string is the first character of the second string and so on.

The method charAt(int index) returns the character at the specified index(IT IS A GETTER NOT A SETTER). You cannot use it as merge.charAt(i) = s.charAt(i/2)

one of the easiest ways to perform such operation is to use string operation such as concatination as shown in the below example


Append a single character to a string or char array in java?, We can also join a list of elements using join() method. In Java 8 we have a new Method join() in the Java String class. public static void main(String args[])​{ //Converting an array of String to the list List list<String> = Arrays. String charAt() · String compareTo() · String compareToIgnoreCase() · String contains() · String  Often you may want to split the content of one cell into individual cells, or do the opposite - combine data from two or more columns into a single column. Common examples that require concatenation in Excel are joining names and address parts, combining text with a formula-driven value, displaying dates and times in the desired format, to name

You could simply use the .concact() method without using the for loop:

so your code would look like: merge = s.concat(t);

java, The String class overloads that operator and it is used to concatenate two strings: String aplusb On the other hand, if we try to use the concat() method with a different data type: String str1 Indexing of a String starts at 0, like we're used to with arrays. charAt(int index) returns the character value at a given index. indexOf​()  The concat() method is used to merge two or more arrays and is built directly into the Node.js language. It doesn't change anything about the existing arrays and just simply combines them into one new array.

try this : concatenate two string without inbuilt method and without + operator.

public static void main(String[] args) {
    String s = "ABC";
    String s1 = "DEF";
    String merge = "";
    char[]ch = new char[120];

    for(int i=0;i<s.length();i++) {
        ch[i] = s.charAt(i);

    for(int i = 0;i<s1.length();i++) {
        ch[s.length()+i] = s1.charAt(i);


Java String join() method explained with examples, merge() in C++ STL · Sorted merge in one array · Merge 3 Sorted Arrays · In-​Place Merge Sort · Merge two sorted arrays in Python using heapq  Table of Content. One Dimensional Array All Programs C Program to Read integers into an array and Reversing them using Pointers; C Program to Merge Two arrays

Common String Operations in Java, The Java String concat() method concatenates one string to the end of This method returns a string with the value of the string passed into the combination of two strings is returned. first_page Java String charAt() method with example to Add two Matrices · How to implement our own Dynamic Array class in Java? Input text, specified as character arrays, cell arrays of character vectors, or string arrays. When combining string or cell arrays with character arrays, the string or cell arrays must be either scalars or column vectors with the same number of rows as the character arrays.

Alternatively Merge two Strings in Java, Characters are of type char and can have one of 2^16 possible values. The charAt() method extracts a specified character from a string in constant time. We avoid forming a string by appending one character at a time because that is a that we consider can work with a low-level representation such as an array of char  For the iterative approach we merge one document at a time, as this results in multiple merge requests, the number of operations, and time taken, increases. To overcome the limitation of the iterative approach we used the REST API approach, which combines all operations into a single request, and is not subject to Flow’s 100MB size limitation .

Java String concat() with examples, One accessor method that you can use with strings is the length() method, which returns the number charAt(i); } // reverse array of chars for (int j = 0; j < len; j++) { charArray[j] The String class includes a method for concatenating two strings:. Using the String class static join method. The .NET Framework contains a class in the System namespace named String. The String class contains a number of methods and a few properties and other things. Complete documentation for the String Class resides on MSDN. A number of the methods of the String class are static methods. This means that they are always available, and that I do not have to create a string (an instance of the class) to gain access to the method.

  • One problem is that merge starts as an empty string, so when you try to get the charAt any index, it will not be able to and thus throw an error. Note that this algorithm actually shouldn't need you to read anything from merged. See where you can get with this advice
  • String is immutable
  • this is exactly what I want, thanks
  • its really brilliant man, you solved another problem for me. when s length is bigger than t length. I had solution but it was long one. my mind was flying around the same solution of yours but didn't reach it. thanks
  • @husseinalbirouti nice to help !
  • I have question: StringBuilder getting characters from s then from t but how it works with StringBuilder without select the index where the characters going to set in. its like starting from the next index always??
  • @husseinalbirouti StringBuilder's .append() method take care of that, it appends the string representation of the char which come from charAt() to its sequence.