How do I concatenate newlines in Java?

java line break in code
newline character java
how to print next line in java
enter in java
r in java
/n in java
java find line break in string
java split string by newline

I've got a very strange question. Say I am implementing an object that has a toString() method that returns a single string formatted in a square, as such:

010203
040506
070809

Now, clearly the entire string, when listed character-wise (and excluding the terminating \0), is as such:

010203\n040506\n070809\n

How would I go about concatenating entire square blocks like these? For instance, if I wanted to have:

010203     101112    010203101112
040506  +  131415 =  040506131415
070809     161718    070809161718

How could I do this? Note that I cannot import Java.util or any other classes.

Edit: The example above was merely an example; ideally I'd like the solution to be extensible to an arbitrarily large number of 'square objects', where I could just call a hypothetical aSquareObject.squareConcat(otherSquareObject).squareConcat(anotherSquareObject).....squareConcat(finalSquareObject) method and it'd spit out two, three, five, fifteen squares side by side.

public class T27Concat {

public static void main(String[] args) {
    String s1 = "010203\n040506\n070809";
    String s2 = "101112\n131415\n161718";

    String plus = "   \n + \n   ";
    String equals = "   \n = \n   ";

    System.out.println(concat(concat(concat(concat(s1, plus), s2), equals), concat(s1, s2)));
}

public static String concat(String s, String t) {
    String[] ss = s.split("\n");
    String[] tt = t.split("\n");
    String result = "";
    for (int i = 0; i < ss.length; i++) {
        result += ss[i] + tt[i] + "\n";
    }
    return result.substring(0, result.length() - 1);
}

}

Is there an easy way to concatenate several lines of text into a string , Here's a Java 8 solution using a stream: This will not append the new line character, but you can certainly append that as well for each line. Newlines. Two strings have no newlines. We can compose them into two lines by concatenating a newline string. In Java we can add newlines in many ways.

public class multiDarr {

    public static void main(String[] args) {


        String str1 = "010203\n040506\n070809\n";
        String str2 = "010203\n040506\n070809\n";

        String ttl = "";



        for(int i = 0;i<str1.length();i= i+7){
            ttl = ttl + str1.substring(i,i+6);
            ttl = ttl +str2.substring(i,i+6);
            ttl = ttl+'\n';

        }
        System.out.println(ttl);
        //System.out.println(ttl);




    }


}

you have to tune i = i+ n for your need , i put 7 because your original string have 6 chars,

result - 010203010203 040506040506 070809070809

Java Newline Examples: System.lineSeparator, Newlines. Two strings have no newlines. We can compose them into two lines by concatenating a newline string. In Java we can add newlines in many ways. I am trying to concatenate set of Text values with new line in between. I can't find way to add new line, tried "\ ", which does not works. I am using Concatenate function.

I attempted solving this on paper, and arrived at a similar solution to that of @mayamar's:

 public String sqConcat(String string1, String string2)
 {
    String[] rows1 = string1.split("\n");
    String[] rows2 = string2.split("\n");

    return new String(rows1[0] + rows2[0] + "\n"
                    + rows1[1] + rows2[1] + "\n"
                    + rows1[2] + rows2[2] + "\n");
 }

Thanks for the help, everyone.

How to append a newline to StringBuilder, There are following two ways to append a new Line to a StringBuilder object: 1) StringBuilder.append("n"); 2) A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are primitive data types and immutable, which means they are unchanging. As strings are the way we display and work with text, and

Java - How to append a newline to StringBuffer, When we append the content to a StringBuffer object, the content gets appended to the end of sequence without any spaces and line breaks. For example: Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Jobs Programming and related technical career opportunities

Add a newline to a String if not empty in Java, append('\n'); } } String toString = builder.toString();. I am suggesting the usage of a StringBuilder here since the kind of String concatenation in� Let libraries do this for you. import com.sun.deploy.util.StringUtils; as well as many others have StringUtils, which has a join method. You can do this in one line: StringUtils.join(list, DELIMITER); But for more context, here is how you could do it with a hashmap.

Java - How to Split String by New Line, In Java, we can use regex \\r?\\n to split a String by new line. sb.append("ddd\r\ n"); sb.append("\r\n"); sb.append("eee\n"); String text = sb. The size of the new dimension is one more than the number of newlines in a string element. splitlines assigns the results of the split along the new dimension. For example, if str is a 2-by-3 string array, and each string has three newline characters, then newStr is a 2-by-3-by-4 array. If the input array str is a string array, then so is newStr.

Comments