I'm trying to concatenate a variable into a literal string purely for readability purposes e.g.
myString = "test"
myString2 = [[
first part of the string
this is a " .. myString .. " string
last part of the string]]
print(myString2)
but this literally outputs
first part of the string
this is a " .. myString .. " string
last part of the string
I'm sure it's something simple but I've tried Googling to find out how to achieve this and came up blank.
The quotes inside the double bracket delimitors aren't doing anything. The only way to end the double bracket is with a double bracket:
myString2 = [[
first part of the string
this is a ]] .. myString .. [[ string
last part of the string]]
That will give you:
first part of the string
this is a test string
last part of the string
See: http://www.lua.org/pil/2.4.html
Related
Code:
path5 = '2.Project\WP101 (237641784)\QR2\5.Project\\'
print ('path5 =',path5)
I get:
path5 = 2.Project\WP101 (237641784)\QR2.Project\
What can I do to stop getting the weird sign after QR2 in the path name?
You need to ignore escape chars.
#ignoring escape sequences
#ignoring single quote escape sequences
str1 = r"Hi, I\'m IncludeHelp"
#ignoring double quotes escape sequences
str2 = r"\"Hello world\""
#ignoring path escape sequences
str3 = r"D:\\work_folder\\python_works"
#ignoring hexadecimal values escape sequences
str4 = r"This is \x49\x6E\x63\x6C\x75\x64\x65\x48\x65\x6C\x70"
print(str1);
print(str2);
print(str3);
print(str4);
Just put an r infront of your string and you are good to go.
In your case:
path5 = r'2.Project\WP101 (237641784)\QR2\5.Project\'
print ('path5 =',path5)
I want to split a str by more than one character but I keep getting errors. Is this even possible? A brief search seems to imply it's not.
I want to do something like this:
let strspl = "test="
let spl : [String] = str.split(separator: Character(strspl), maxSplits: 1).map(String.init)
Error is:
Can't form a Character from a String containing more than one extended grapheme cluster
You can use components(separatedBy:) method to get an array of string by separating a string with a string separator
var str = "another test= long test"
let strspl = "test="
let spl : [String] = str.components(separatedBy: strspl)
print(spl)
["another ", " long test"]
I am trying to print a string in formatted like " FOO"
in Swift 4. I can do it with chars but I can't do it with a string object.
I tried:
let a = String(format: "%28c", 0x31)
print (a)
// prints " 1"
But this only works with chars, not with strings.
How can I get the same result with strings?
I have a question about string concatenation.
I have this example where I am trying to append a value inside a json block with a variable value
example:
clock = os.clock()
body = "{\"name\":\"stringValue\" .. clock }"
print(body)
When I run this I get the following output:
{"name":"stringValue" .. clock }
What I am expecting is something like
{"name":"stringValue0.010117"}
How do I make is so this variables value is added to the string?
This is an instance were using [[ ]] delimited strings is useful:
clock = os.clock()
body = [[{"name":"stringValue]] .. clock .. [["}]]
print(body)
To continue using a double quoted string, your variable assignment would look like the following (note how the quote after stringValue is not escaped):
body = "{\"name\":\"stringValue" .. clock .. "\"}"
Is there anyway to use a string literal as an argument to a function within a println statement.
func greetings(name: String) -> String {
return "Greetings \(name)!"
}
What I was trying to do: (I tried escaping the quotes around Earthling.)
println("OUTPUT: \(greetings("Earthling"))")
You can alternatively do this:
let name = "Earthling"
println("OUTPUT: \(greetings(name))")
And this works too:
println(greetings("Earthling"))
I tried escaping the quotes in the first example but with no luck, its not super important as its only a test, I was just curious if there was a way to do this, using a function call with a string literal as an argument within a print or println statement that contains other text.
From the Apple 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.
The problem is of course not with println but with the embedding of expressions with quotes in string literals.
Thus
let b = false
let s1 = b ? "is" : "isn't"
let s2 = "it \(b ? "is" : "isn't")" // won't compile
However NSLog as a one-liner'' works quite well here
NSLog("it %#", b ? "is" : "isn't")
Note %#, not %s. Try the latter in a playground to see why.