Swift 4
. , %s, Unicode, emojis "ä", "ö", "ü", "ß".
%s Swift:
: String(format: String, arguments: CVarArg...)
1. arguments: CVarArg...
,
let stringToFormat = "test"
let formattedString = stringToFormat.withCString{
String(format: "%s", $0)
}
, , .
...
2. Various lines in arguments: CVarArg...
The easiest way to find an extension Stringwith a computed propertyc
extension String {
struct CString: CVarArg {
var _cVarArgEncoding: [Int] = []
var cstring: ContiguousArray<CChar> = []
init(string: String) {
cstring = string.utf8CString
self._cVarArgEncoding = cstring.withUnsafeBufferPointer{
$0.baseAddress!._cVarArgEncoding
}
}
}
var c: CString {
return CString(string: self)
}
}
Using
let stringToFormat1 = "test1"
let stringToFormat2 = "test2"
let formattedString = String(format: "%s %s %s", stringToFormat1.c, stringToFormat2.c, "test3".c)
Your specific problem
Using the second solution:
var str = String(format: "%-12s - %s", "key".c, "value".c)
Qbyte source
share