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.
Related
I am working on a project, in which you type your input sentence, and I need to be able to use " and ' in the sentence, such as Input = "I said, "Hi what's up?" print(Input) in which I get an error. If anyone knows how to fix this that would be great.
See https://www.lua.org/pil/2.4.html. Lua has very interesting feature to declare string with square brackets:
input = [[I said, "Hi what's up?"]]
input = "I said, \"Hi what's up?\""
input = 'I said, "Hi what\'s up?"'
I will tell some things in addition to what #Darius told above
When you tried to add a quatation mark inside a string, the lua interpreter get confused and break your string after the next quation mark without reaching the end of the line. That's the reason for the error.
Try to understand it by the following code
str = "Hello I"m somebody" -- here the interpreter will think str equals to "Hello I" at first, and then it will find some random characters after which may make it confused (as m somebody is neither a variable nor a keyword)"
-- you can also see the way it got confused by looking at the highlighted code
--What you can do to avoid this is escaping the quotes
str = "Hello I\"m somebody" -- here the interpreter will treat \" as a raw character (") and parse the rest.
You can also use the escape character () with others such as \', \", \[, \n (newline character), \t (tab) and so on.
I am working on string manipulation using LUA and having trouble with the following problem.
Using this as an example of the original data I am given -
"[0;1;36m(Web): You say, "Text here."[0;37m"
I want to keep the string intact except for removing the ANSI codes.
I have been pointed toward using gsub with the LUA pattern matching but I cannot seem to get the pattern correct. I am also unsure how to reference exactly the escape character sent.
text:gsub("[\27\[([\d\;]+)m]", "")
or
text:gsub("%x%[[%d+;+]m", "")
If successful, all I want to be left with, using the above example, would be:
(Web): You say, "Text here."
Your string example is missing the escape character, ASCII 27.
Here's one way:
s = '\x1b[0;1;36m(Web): You say, "Text here."\x1b[0;37m'
s = s:gsub('\x1b%[%d+;%d+;%d+;%d+;%d+m','')
:gsub('\x1b%[%d+;%d+;%d+;%d+m','')
:gsub('\x1b%[%d+;%d+;%d+m','')
:gsub('\x1b%[%d+;%d+m','')
:gsub('\x1b%[%d+m','')
print(s)
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 an answer here:
Ruby string prepend '\' character
(1 answer)
Closed 7 years ago.
I tried storing a string in rails like
string = 'abc#$123'
but the string stores "abc\ #$123". I tried removing "\" by using string.delete("\",'') but didn't work
Is there any way to solve this problem ?
This is correct, Ruby interpreter is just escaping the character #$ by using backslash (\) character.
It is not exactly changing your string and adding the unwanted (\) character. You can verify this by doing puts string and it should print abc#$123
The "\" is a way to escape the # character, which has a special meaning in ruby. So the "\" does not actually exist, it is just a convenience used by ruby to store your string value.
So, don't bother about it. You will see that if you print your string, the "\" will magically disappear.
irb(main):001:0> s = 'abc#$123'
=> "abc\#$123" # internal representation of your string
irb(main):002:0> print s
abc#$123=> nil # printed string value
I've been playing around with Swift, and just came across an issue.
I have the following Dictionary in:
var locations:Dictionary<String,CLLocationCoordinate2D> = ["current":CLLocationCoordinate2D(latitude: lat, longitude: lng) ];
println("current locaition is \(locations["current"])")
but the compiler is complaining about double quotes around current which represent the a key in my dictionary.
I tried escaping it with \ but it wasn't the right way.
Appreciate any help.
Xcode 7.1+
Since Xcode 7.1 beta 2, we can now use quotations within string literals. From the release notes:
Expressions interpolated in strings may now contain string literals. For example, "My name is (attributes["name"]!)" is now a valid expression. (14050788)
Xcode <7.1
I don't think you can do it that way.
From the docs
The expressions you write inside parentheses within an interpolated string cannot contain an unescaped double quote (") or backslash (\), and cannot contain a carriage return or line feed.
You'd have to use
let someVar = dict["key"]
println("Some words \(someVar)")
You can use string concatenation instead of interpolation:
println("Some words " + dict["key"]! + " some more words.")
Just mind the spaces around the + signs.
UPDATE:
Another thing you can do is to use string format specifiers the same way how it was done back in the objective-c days:
println( String(format: "Some words %# some more words", dict["1"]!) )
I've had this problem too with interpolating strings. The best solution I found was just to split it into two lines like so:
let location = locations["current"]
println("current locaition is \(location)")
It may be a bug with Swift. From what I found in the docs, you should be able to use \ to escape quotes.
Swift doesn't accept quote in \(). Therefore you need to separate the one-line code into two.
Here is a blog showing example for Chinese readers:
http://tw.gigacircle.com/321945-1
Since Swift 2.1 you can use double quotes on interpolation. println("current location is \(locations["current"])")