如何转换 Swift 中的字符串?
How to convert Strings in Swift?
我有两种情况需要将字符串转换为不同的格式。
例如:
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.
但是当我使用这段代码时:
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"];
实际用法:
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.
其次如何替换 swift 中的转义字符:我在 replaceOccurances(of:"\'" with:"'");
中使用了“\”,但它没有改变。为什么以及如何逃避该序列。
case 2:
string input: \'abc\'
output: 'abc'
案例 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)
案例 2:
var str = "\'abc\'"
print(str.replacingOccurrences(of: "\'", with: ""))
要将字典的所有值作为数组获取,您可以使用字典的 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]
使用 filter
你可以忽略字典中所有非 String
:
类型的值
let stringValues = values.filter({ [=11=] is String }) as! [String]
// stringValues: ["value_a", "value_b", "value_c", "value_d"]
使用地图,您可以转换 stringValues
的值并应用您的 replacingOccurrences
函数:
let adjustedValues = stringValues.map({ [=12=].replacingOccurrences(of: "value_", with: "") })
// adjustedValues: ["a", "b", "c", "d"]
为什么不尝试这样的事情呢?对于问题的第 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)
此外,第 2 部分似乎很简单,除非我没记错
var escaped: String = "\'abc\'"
print(escaped)
我有两种情况需要将字符串转换为不同的格式。
例如:
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.
但是当我使用这段代码时:
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"];
实际用法:
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.
其次如何替换 swift 中的转义字符:我在 replaceOccurances(of:"\'" with:"'");
中使用了“\”,但它没有改变。为什么以及如何逃避该序列。
case 2:
string input: \'abc\'
output: 'abc'
案例 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)
案例 2:
var str = "\'abc\'"
print(str.replacingOccurrences(of: "\'", with: ""))
要将字典的所有值作为数组获取,您可以使用字典的 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]
使用 filter
你可以忽略字典中所有非 String
:
let stringValues = values.filter({ [=11=] is String }) as! [String]
// stringValues: ["value_a", "value_b", "value_c", "value_d"]
使用地图,您可以转换 stringValues
的值并应用您的 replacingOccurrences
函数:
let adjustedValues = stringValues.map({ [=12=].replacingOccurrences(of: "value_", with: "") })
// adjustedValues: ["a", "b", "c", "d"]
为什么不尝试这样的事情呢?对于问题的第 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)
此外,第 2 部分似乎很简单,除非我没记错
var escaped: String = "\'abc\'"
print(escaped)