create your own

Java Programming: How to insert a new line in a String

74
rate or flag this page

By highway star

Why do you need to insert a new line in a String?

Often, when programming it is useful to manipulate Strings in a way that you need to put a new line in an already existing string or a in a string that you are building. To do this you just need to know that Java makes everything very easy for you and the only thing needed is the concept of line separator and how to get it in Java. In this page you will learn how to insert a new line in a string in Java in just a few lines of code.


Inserting a new line during string construction

Suppose you are reading the data from an xml file containing generalities about persons and you want to print the name, surname and birth date on separate lines. Then to do this, you just need to write the following lines:

String endofline = System.getProperty("line.separator");
myString = "Name: " + childElement.getAttributeValue("Name") + endofline
+ "Surname: " + childElement.getAttributeValue("Surname") + endofline
+ "Birth: " + childElement.getAttributeValue("Birth") + endofline;

As we can see, the first operation is to get the line separator in a String called endofline and then append this to the various points where we want to insert it.

Inserting a new line in an existing String

Suppose your string is called myString and you need to insert a new line at the end of each record ending with "),". To do this you just need the following in Java.

String endofline = System.getProperty("line.separator");

myString = myString.replace("),",")," + endofline)

To print the string in the console just type:

System.out.println(myString);

and you will see that every record ending with ")," is replaced with ")," and a new line.


Effective Java (2nd Edition) Effective Java (2nd Edition)
Price: $31.46
List Price: $54.99
Head First Java, 2nd Edition Head First Java, 2nd Edition
Price: $28.00
List Price: $44.95
Java Concurrency in Practice Java Concurrency in Practice
Price: $30.00
List Price: $59.99
SCJP Sun Certified Programmer for Java 6 Exam 310-065 SCJP Sun Certified Programmer for Java 6 Exam 310-065
Price: $31.49
List Price: $49.99
Core Java, Vol. 2: Advanced Features, 8th Edition Core Java, Vol. 2: Advanced Features, 8th Edition
Price: $30.00
List Price: $59.99
Core Java(TM), Volume I--Fundamentals (8th Edition) Core Java(TM), Volume I--Fundamentals (8th Edition)
Price: $34.96
List Price: $59.99
Java Web Services: Up and Running Java Web Services: Up and Running
Price: $21.51
List Price: $34.99
Java In A Nutshell, 5th Edition Java In A Nutshell, 5th Edition
Price: $28.19
List Price: $44.95
Big Java Big Java
Price: $48.03
Beginning Programming with Java For Dummies (For Dummies (Computer/Tech)) Beginning Programming with Java For Dummies (For Dummies (Computer/Tech))
Price: $4.99
List Price: $24.99

Print   —   Rate it:  up  down  flag this hub

Comments

RSS for comments on this Hub

No comments yet.

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working