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.'
Related
I have a string like this in below and I want replace space with backslash and space.
let test: String = "Hello world".replacingOccurrences(of: " ", with: "\ ")
print(test)
But Xcode make error of :
Invalid escape sequence in literal
The code in up is working for any other character or words, but does not for backslash. Why?
Backslash is used to escape characters. So to print a backslash itself, you need to escape it. Use \\.
For Swift 5 or later you can avoid needing to escape backslashes using the enhanced string delimiters:
let backSlashSpace = #"\ "#
If you need String interpolation as well:
let value = 5
let backSlashSpaceWithValue = #"\\#(value) "#
print(backSlashSpaceWithValue) // \5
You can use as many pound signs as you wish. Just make sure to mach the same amount in you string interpolation:
let value = 5
let backSlashSpaceWithValue = ###"\\###(value) "###
print(backSlashSpaceWithValue) // \5
Note: If you would like more info about this already implemented Swift evolution proposal SE-0200 Enhancing String Literals Delimiters to Support Raw Text
Due to the document of glib.string.escape()
Escapes the special characters '\b', '\f', '\n', '\r', '\t', '\v', '\' and '"' in the string source by inserting a '\' before them.
Additionally all characters in the range 0x01-0x1F (everything below SPACE) and in the range 0x7F-0xFF (all non-ASCII chars) are replaced with a '\' followed by their octal representation. Characters supplied in exceptions are not escaped.
Now I want not eacape "0x7F-0xFF" characters. How to write the exceptions part?
my example code no work.
shellcmd = "bash -c \""+file.get_string(title,"List").escape("0x7F-0xFF")+"\"";
print("shellcmd: %s\n", shellcmd);
Process.spawn_command_line_sync (shellcmd,
out ls_stdout, out ls_stderr, out ls_status);
if(ls_status!=0){ list = ls_stderr.split("\n"); }
else{ list = ls_stdout.split("\n"); }
this works.
shellcmd = "bash -c \""+file.get_string(title,"Check").replace("\"","\\\"")+"\"";
You actually have to put the characters 0x7f to 0xff in the exceptions argument. So something like:
shellcmd = "bash -c \""+file.get_string(title,"List").escape("\x7F\x80\x81\x82…\xfe\xff")+"\"";
You would need to list them all manually.
Looking more generally at your code, you seem to be constructing a command to run. This is a very bad idea and you should never do it. It is wide open to code injection. Use Process.spawn_sync() and pass it an argument vector instead.
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.
Hell, I am new to play framework and Scala. I don't know how to display special characters like #, -, "" as text.
Help!
If I got your question right, you are asking how to escape special characters in scala. It's pretty much same as other languages. using escape character \
val str = " \\\" " //output str: \"
or follow the below special syntax to store in string value with exactly what is inside the triple quotes.
val str = """ \" """ //output str: \"
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.