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.
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.
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.
Related
I am new to spark. I have a huge file which has data like-
18765967790#18765967790#T#20130629#00#31#2981546 " "18765967790#18765967790#T#20130629#19#18#3240165 " "18765967790#18765967790#T#20130629#18#18#1362836
13478756094#13478756094#T#20130629#31#26#2880701 " "13478756094#13478756094#T#20130629#19#18#1230206 " "13478756094#13478756094#T#20130629#00#00#1631440
40072066693#40072066693#T#20130629#79#18#1270246 " "40072066693#40072066693#T#20130629#79#18#3276502 " "40072066693#40072066693#T#20130629#19#07#3321860
I am trying to replace " " with new line character so that my output looks like this-
18765967790#18765967790#T#20130629#00#31#2981546
18765967790#18765967790#T#20130629#19#18#3240165
18765967790#18765967790#T#20130629#18#18#1362836
13478756094#13478756094#T#20130629#31#26#2880701
13478756094#13478756094#T#20130629#19#18#1230206
13478756094#13478756094#T#20130629#00#00#1631440
40072066693#40072066693#T#20130629#79#18#1270246
40072066693#40072066693#T#20130629#79#18#3276502
40072066693#40072066693#T#20130629#19#07#3321860
I have tried with-
val fact1 = sc.textFile("s3://abc.txt").map(x=>x.replaceAll("\"","\n"))
But this doesn't seem to be working. Can someone tell what I am missing?
Edit1- My final output will be a dataframe with schema imposed after splitting with delimeter "#".
I am getting below o/p-
scala> fact1.take(5).foreach(println)
18765967790#18765967790#T#20130629#00#31#2981546
18765967790#18765967790#T#20130629#19#18#3240165
18765967790#18765967790#T#20130629#18#18#1362836
13478756094#13478756094#T#20130629#31#26#2880701
13478756094#13478756094#T#20130629#19#18#1230206
13478756094#13478756094#T#20130629#00#00#1631440
40072066693#40072066693#T#20130629#79#18#1270246
40072066693#40072066693#T#20130629#79#18#3276502
40072066693#40072066693#T#20130629#19#07#3321860
I am getting extra blank lines which is further troubling me to create dataframe. This might seem simple here, but the file is huge, also the rows containing " " are long. In the question I have put only 2 double quotes but they can be more than 40-50 in numbers.
There are more than one quote in between textes, which is creating multiple line breaks. You either need to remove additional quotes before replace or empty lines after replace:
.map(x=>x.replaceAll("\"","\n").replaceAll("(?m)^[ \t]*\r?\n", ""))
Reference: Remove all empty lines
You might be missing implicit Encoders and you try the code as below
spark.read.text("src/main/resources/doubleQuoteFile.txt").map(row => {
row.getString(0).replace("\"","\n") // looking to replace " " with next line
row.getString(0).replace("\" \"","\n") // looking to replace " " with next line
})(org.apache.spark.sql.Encoders.STRING)
In the following line of code, what is the backslash telling Swift to do?
print("The total cost of my meal is \(dictionary["pizza"]! + dictionary["ice cream"]!)")
The backslash has a few different meanings in Swift, depending on the context. In your case, it means string interpolation:
print("The total cost of my meal is \(dictionary["pizza"]! + dictionary["ice cream"]!)")
...is the same as:
print("The total cost of my meal is " + String(dictionary["pizza"]! + dictionary["ice cream"]!))
But the first form is more readable. Another example:
print("Hello \(person.firstName). You are \(person.age) years old")
Which may print something like Hello John. You are 42 years old. Much clearer than:
print("Hello " + person.firstName + ". You are " + String(person.age) + " years old")
That's called String interpolation. When you want to embed the value of a variable in a String, you have to put the variable name between parentheses and escape the opening parentheses with a backslash. This way the compiler knows that it has to substitute the value of the variable there instead of using the String literal of the variable name.
For more information on the topic, have a look at the String interpolation part of the Swift Language Guide.
I'm new to Scala.
Is it possible to force using a semicolon as the end of a line ?
e.g.
val s = "my line"
+ " ends here";
Thanks
You don't want to "force using a semicolon", quite the contrary: you want to avoid that a semicolon is inferred at the end of the first line.
Several possibilities here:
Move plus to previous line (that's the preferred way to do it):
val s = "my line" +
"ends here";
Explicit method calls starting with . prevent the semicolon from being inferred (this works reasonably well for "builder-pattern" like chains of methods, but looks ugly for +):
val s = "my line"
.+("ends here");
Add parentheses. Semicolons are never inferred inside parentheses:
val s = ("my line"
+ "ends here");
How come that this string
"answer
to life
the universe
and everything
is
#{40+2}
"
compiles into
" answer to life the universe and everything is " + (40 + 2) + "";
how can I force coffescript to keep it multiline (keeping string interpolation intact):
"answer \
to life \
the universe \
and everything \
is \
"+(40+2)
Try using the heredoc syntax:
myString = """
answer
to life
the universe
and everything
is
#{40+2}
"""
This converts to this javascript:
var myString;
myString = "answer\nto life\nthe universe\nand everything\nis\n" + (40 + 2);
There's not really any point to make it actually be on newlines in the compiled javascript visually, is there?
I agree it is nice to be able to keep your indentation when defining long strings. You can use string addition for this effect in coffeescript just like you can in javascript:
myVeryLongString = 'I can only fit fifty-nine characters into this string ' +
'without exceeding eighty characters on the line, so I use ' +
'string addition to make it a little nicer looking.'
evaluates to
'I can only fit fifty-nine characters into this string without exceeding eighty characters, so I use string addition to make it a little nicer looking.'
I am fairly new to scala and I have the need to convert a string that is pipe delimited to one that is comma delimited, with the values wrapped in quotes and any quotes escaped by "\"
in c# i would probably do this like this
string st = "\"" + oldStr.Replace("\"", "\\\\\"").Replace("|", "\",\"") + "\""
I haven't validated that actually works but that is the basic idea behind what I am trying to do. Is there a way to do this easily in scala?
Similarly:
val st = "\"" + oldStr.replaceAll("\"", "\\\\\"").replaceAll("\\|", "\",\"") + "\""
Could also be:
val st = oldStr.replaceAll("\"","\\\\\"").split("\\|").mkString("\"","\",\"","\"")