Jeremy
Jeremy

Reputation: 975

Scala new line without regex?

I am trying to insert a new line into a string in scala. I've been reading around and it seems to not be as straight forward as java or C++.

my problem is i have a class called usedCar and Im overriding the toString method which is a 1 liner.

override def toString = ("Make: " + _carMake +  "\nYear of Car: " + _yearOfCar + "\nVin Number: " + _vinNumber + "\nOdometer Mileage: " + _odometerMileage + "\nCondition of Car: " + _carCondition + "\nBest Price: " + _bestPrice + "\nAsking Price: " + askingPrice);

if someone can tell me how to get new lines in that toString method I would greatly appreciate it.

Upvotes: 0

Views: 1454

Answers (2)

user177800
user177800

Reputation:

The escape code in Java for a new line is \n.

In your original question before the edit you have /n which wasn't correct.

The idiomatic way to do this is to assign sys.props("line.separator") to a variable and append that instead.

A more modern idiomatic way to do this would be to use String.format() and build your String that way and supply the sys.props("line.separator") as well.

Upvotes: 3

We Are All Monica
We Are All Monica

Reputation: 13344

You're using the wrong kind of slash. You want a backslash (\n) not a forward slash (/n). The question has since been edited.

And if you are on Windows, you really want \r\n instead. There are better ways of deciding which one to use, as Jarrod mentions in his answer.

Upvotes: 1

Related Questions