Deepak
Deepak

Reputation: 302

Is there any simpler code to separate lines?

I am using this code to separate the next line and giving space.

String sms="Name:"+name+ System.getProperty ("line.separator")+System.getProperty   
   ("line.separator")+"ContactNumber:"+contactnumber+ System.getProperty 
   ("line.separator")+"Quantity:"+quantity+System.getProperty 
   ("line.separator")+"Number.of.Pcs:"+noofpieces+System.getProperty 
   ("line.separator")+"Date and Time:"+dateandtime
   +System.getProperty ("line.separator")+"Delivary 
   Address:"+deliveryaddress;

Upvotes: 0

Views: 305

Answers (4)

blackpanther
blackpanther

Reputation: 11486

You could use a StringBuilder instance and then use the new line operator appended to the StringBuilder. For example:

StringBuilder sb = new StringBuilder();
sb.append("Name: ").append(name);
sb.append("\n"); // for a new line.

Whatever the case, I would strongly recommend that you use a StringBuilder to append to a very large String.

In addition, you could also use System.lineSeparator(); However, that may only work in Java with the JVM with Java 7 and not in Android (so I would definitely check that out.)

Upvotes: 2

SubOptimal
SubOptimal

Reputation: 22983

You could also use this solution

String format = "Name: %s%n%nContactNumber: %s%nQuantity: %s%nNumber.of.Pcs: %s%nDate and Time: %s%nDelivery Address: %s";
String sms = String.format(format, name, contactnumber, quantity, noofpieces, dateandtime, deliveryaddress);

The explanation of the format placeholders you find in the Javadoc for java.util.Formater

Upvotes: 0

njzk2
njzk2

Reputation: 39406

Using System.getProperty("line.separator") is a good practice as it will give you code that could be reused on another platform. To simplify your code, you can use TextUtils.join :

String sms = TextUtils.join(System.getProperty("line.separator"), 
    new String[] {
        "Name:" + name , 
        "ContactNumber:" + contactnumber, 
        ...});

Upvotes: 1

betorcs
betorcs

Reputation: 2831


String sms= "Name:" + name 
  + "\nContactNumber:" + contactnumber
  + "\nQuantity:" + quantity
  + "\nNumber.of.Pcs:" + noofpieces
  + "\nDate and Time:" + dateandtime
  + "\nDelivary Address:" + deliveryaddress;

Upvotes: 1

Related Questions