The question is simple. I just tried to print the \ character using this code:
print("\")
But an error has occurred saying that it's an unfinished string.
I'm using Lua 5.2 version.
Backslashes are used for escape sequences, you should print 2 backslashes, like this:
print("\\")
See this for more details on escaping strings, and just strings in general.
Related
How to print all special characters without inserting escape sign before every of them? I have very large textiles with many special characters and I'm looking for something like # in c# which prints string literally as it is
What you're referring to, is called a verbatim string literal in C# and that concept does not translate exactly to Swift.
However, with the introduction of multiline string Literals in Swift 4, you can get close.
let multilineString = """
Here you can use \ and newline characters.
Also single " or double "" are allowed.
"""
For reference, find the grammar of a Swift String literal here.
This question already has answers here:
(Swift) how to print "\" character in a string?
(4 answers)
Closed 7 years ago.
How do you concatenate a backslash in swift?
i.e. "some string" + "\"
escaping the backslash gives me "some string\\" but I want "some string\"
Any ideas on how to accomplish this?
EDIT: I don't want to print out the string, I just want to concatenate the backslash. Escaping the backslash will store the string with two backslashes but I only want one.
EDIT 2: I think I figured it out. I used "\"" and that seems to work for me.
The double backslash solution is correct (see console output in my small sample)
Duplicate
(Swift) how to print "\" character in a string?
But..
If you're in a playground and type:
print("\\Hello, World")
... then yes both slashes and even a newline character appear in the results to the right as:
\\Hello, World\n
but that's not the same as actual output.
If you open the debug console (Cmd-Shit-Y), or click the tiny eyeball icon that shows you the output, you'll see that there's only one \ and the escape works as expected.
For concantenation & string interpolation you could do...
var string = "some string"
print("\(string)\\") //prints "some string\"
Or...
string +="\\"
print(string) //also prints "some string\"
Again, the right-column preview output is slightly different, so you have to look at the console. This is a somewhat confusing & annoying feature of Playgrounds. Not sure why they don't ensure both preview & console output are the same, or show the debug console by default.
I want to define an array in ruby in following manner
A = ["\"]
I am stuck here for hours now. Tried several possible combinations of single and double quotes, forward and backward slashes. Alas !!
I have seen this link as well : here
But couldn't understand how to resolve my problem.
Apart from this what I need to do is -
1. Read a file character by character (which I managed to do !)
2. This file contains a "\" character
3. I want to do something if my array A includes this backslash
A.includes?("\")
Any help appreciated !
There are some characters which are special and need to be escaped.
Like when you define a string
str = " this is test string \
and this contains multiline data \
do you understand the backslash meaning here \
it is being used to denote the continuation of line"
In a string defined in a double quotes "", if you need to have a double quote how would you doo that? "\"", this is why when you put a backslash in a string you are telling interpretor you are going to use some special characters and which are escaped by backslash. So when you read a "\" from a file it will be read as "\" this into a ruby string.
char = "\\"
char.length # => 1
I hope this helps ;)
Your issue is not with Array, your question really involves escape sequences for special characters in strings. As the \ character is special, you need to first prepend it (escape it) with a leading backslash, like so.
"\\"
You should also re-read your link and the section on escape sequences.
You can escape backslash with a backslash in double quotes like:
["\\"].include?("\\")
I am writing a test and verifying some data. It's failing due to the two \\ I get in the expected data string.
My test code is:
actual_string.should eq 'Today is Tuesday.\n It is third day of the week.'
When I execute this code, I get an error saying the actual data does not match the expected data.
The actual data is:
'Today is Tuesday.\n It is third day of the week.'
The expected data is:
'Today is Tuesday.\\n It is third day of the week.'
Not sure from where is that extra slash '\' is coming from in the expected data. How can I resolve this?
use "Text" - double quotes....
Unlike other languages (e.g. Python, JavaScript etc.), Ruby uses different escape sequences in single-quoted and double-quoted strings.
Single-quoted strings only support \' and \\. Everything else is treated literally. So, '\n' is two characters \ and n, not a single new line character.
To use the new line character, enclose your string into double quotes:
actual_string.should eq "Today is Tuesday.\n It is third day of the week."
This will fix your test.
How do I remove this backslash?
s = "\""
I have tried s.gsub("\\", "") and that doesn't remove it, it returns the same string.
there's actually no backslash character in your String. The Backslash in your example simply escapes the following double quote and prevent's that it would terminate the string and thereby resulting in a syntax error (unterminated double quote ).
So what you see when you print that string in IRB is actually not the backslash as is, but the backslash in combination with the following dobule quote as an indication that the double quote is escaped. Kind of hard to grasp when you encounter it the first time. Have a look at http://en.wikibooks.org/wiki/Ruby_Programming/Strings#Escape_sequences
long story short: there is no backslash in your string so you can't remove it :)
gsub takes a regular expression as the first parameter. I believe that if you pass it a string, it will first convert it into a regex. This means you need extra escaping:
s.gsub("\\\\", "")
If you use regex notation, you can stop it from doubling up:
s.gsub(/\\/, "")
This is because you don't have to escape twice: once because double-quoted strings need you to escape the \ character, and once because the regular expression requires you to as well.
that's actually an escape quote sign (do a print s to see it)
I'm not sure if this is a solution to YOUR problem, but seeing that this is one of the first SO questions I looked at when trying to solve my problem and have in fact, solved it, here is what I did to fix my problem.
So I had some CSV.read output with a load of \ (backslashes) and unwanted quotation marks.
arr_of_arrays = CSV.read("path/to/file.csv")
processed_csv = arr_of_arrs.map {|t| eval(t)}
the key here is the eval() method.