[英]How to convert Strings in Swift?
I've two cases where I need to convert strings to different formats. 我有两种情况需要将字符串转换为不同格式。
for ex: 例如:
case 1:
string inputs: abc, xyz, mno, & llr // All Strings from a dictionary
output: ["abc","xyz", "mno", "llr"] //I need to get the String array like this.
But when I use this code: 但是当我使用此代码时:
var stringBuilder:[String] = [];
for i in 0..<4 {
stringBuilder.append("abc"); //Appends all four Strings from a Dictionary
}
print(stringBuilder); //Output is 0: abc, 1:xyz like that, how to get desired format of that array like ["abc", "xyz"];
Real usage: 实际用法:
let arr = Array(stringReturn.values);
//print(arr) // Great, it prints ["abc","xyz"];
let context = JSContext()
context?.evaluateScript(stringBuilder)
let testFunction = context?.objectForKeyedSubscript("KK")
let result = testFunction?.call(withArguments:arr); // Here when I debugger enabled array is passed to call() like 0:"abc" 1:"xyz". where as it should be passed as above print.
Secondly how to replace escape chars in swift: I used "\\" in replaceOccurances(of:"\\\\'" with:"'");
其次,如何快速替换转义字符:我在
replaceOccurances(of:"\\\\'" with:"'");
but its unchanged. 但其不变。 why and how to escape that sequnce.
为什么以及如何逃避该序列。
case 2:
string input: \'abc\'
output: 'abc'
To get all values of your dictionary as an array you can use the values
property of the dictionary: 要将字典的所有值作为数组获取,可以使用字典的
values
属性:
let dictionary: Dictionary<String, Any> = [
"key_a": "value_a",
"key_b": "value_b",
"key_c": "value_c",
"key_d": "value_d",
"key_e": 3
]
let values = Array(dictionary.values)
// values: ["value_a", "value_b", "value_c", "value_d", 3]
With filter
you can ignore all values of your dictionary that are not of type String
: 使用
filter
您可以忽略字典中不是String
类型的所有值:
let stringValues = values.filter({ $0 is String }) as! [String]
// stringValues: ["value_a", "value_b", "value_c", "value_d"]
With map you can transform the values of stringValues
and apply your replacingOccurrences
function: 有了地图,你可以改变的值
stringValues
并应用replacingOccurrences
功能:
let adjustedValues = stringValues.map({ $0.replacingOccurrences(of: "value_", with: "") })
// adjustedValues: ["a", "b", "c", "d"]
case 1: I have Implemented this solutions, Hope this will solve your problem 案例1:我已经实现了此解决方案,希望这可以解决您的问题
let dict: [String: String] = ["0": "Abc", "1": "CDF", "2": "GHJ"]
var array: [String] = []
for (k, v) in dict.enumerated() {
print(k)
print(v.value)
array.append(v.value)
}
print(array)
case 2: 情况2:
var str = "\'abc\'"
print(str.replacingOccurrences(of: "\'", with: ""))
Why not try something like this? 为什么不尝试这样的事情? For part 1 of the question that is:
对于问题的第1部分,是:
var stringReturn: Dictionary = Dictionary<String,Any>()
stringReturn = ["0": "abc","1": "def","2": "ghi"]
print(stringReturn)
var stringBuilder = [String]()
for i in stringReturn {
stringBuilder.append(String(describing: i.value))
}
print(stringBuilder)
Also, part 2 seems to be trivial unless I'm not mistaken 另外,除非我没有记错,否则第2部分似乎很简单。
var escaped: String = "\'abc\'"
print(escaped)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.