我必须解析这些类型的字符串:
public void showMessage(string message)
{
setMessage(message);
Debug.Log("start_fadeIn");
StartCoroutine(coroutine__fadeIn(delegate
{
Debug.Log("start_fadeOut");
StartCoroutine(coroutine__fadeOut(delegate
{
Debug.Log("done");
}));
}));
}
private IEnumerator coroutine__fadeIn(Action completion)
{
CanvasGroup canvasGroup = GetComponent<CanvasGroup>();
for (float f = 0f; f <= 1; f += 0.01f)
{
canvasGroup.alpha = f;
yield return null;
}
completion();
}
private IEnumerator coroutine__fadeOut(Action completion)
{
CanvasGroup canvasGroup = GetComponent<CanvasGroup>();
for (float f = 1f; f >= 0; f -= 0.01f)
{
canvasGroup.alpha = f;
yield return null;
}
completion();
}
我需要以某种方式提取let result1 = "t=20171017T1201&s=349.00&fn=8712000100030779&i=21456&fp=124519970&n=1"
let result2 = "t=20171016T180757&s=92.00&fn=8710000101140572&i=17711&fp=4191934636&n=1"
和t
参数并将它们保存到另一个字符串:
fn
我设法通过逐符号检查整个字符串来获取这些参数,但必须有更好的方法。
答案 0 :(得分:3)
您需要使用components(separatedBy: _)
将字符串拆分为可解析对。
let result1 = "t=20171017T1201&s=349.00&fn=8712000100030779&i=21456&fp=124519970&n=1"
let result2 = "t=20171016T180757&s=92.00&fn=8710000101140572&i=17711&fp=4191934636&n=1"
func value(for name: String, in query: String) -> String? {
for params in query.components(separatedBy: "&") {
let pair = params.components(separatedBy: "=")
if (pair.first == name) {
return pair.last
}
}
return nil
}
print(value(for: "t", in: result1))
print(value(for: "fn", in: result1))
print(value(for: "t", in: result2))
print(value(for: "fn", in: result2))
答案 1 :(得分:2)
基于这些字符串基本上是URL的查询参数的事实,您可以使用以下hack来轻松获取它:
let result1 = "t=20171017T1201&s=349.00&fn=8712000100030779&i=21456&fp=124519970&n=1"
let result2 = "t=20171016T180757&s=92.00&fn=8710000101140572&i=17711&fp=4191934636&n=1"
let params1 = queryParameters(from: result1)
let params2 = queryParameters(from: result2)
if let t1 = params1["t"], let t2 = params2["t"] {
print(">>> t1 - \(t1) t2 - \(t2)")
}
public func queryParameters(from parameters: String) -> [String: String] {
var components = URLComponents()
components.query = parameters
guard
let queryItems = components.queryItems else {
return [:]
}
var parameters = [String: String]()
for item in queryItems {
parameters[item.name] = item.value
}
return parameters
}
答案 2 :(得分:1)
它会在这里创建一个小的解决方法,URLComponents
在这种情况下可以帮到你,因为你的输入似乎是我的标准查询字符串(参见RFC-3986)。
let result1: String = "t=20171017T1201&s=349.00&fn=8712000100030779&i=21456&fp=124519970&n=1"
所以我会利用它并期望使用URLQueryItem
数组,所以这个扩展已被放在一个数组上(可能没有效果,但很简单)。
extension Array where Iterator.Element == URLQueryItem {
subscript(key: String) -> String? {
get {
return self.filter { $0.name == key }.first?.value
}
}
}
然后我会以随机顺序从随机生成但语法上有效的URL中读取组件:
if let url = URL(string: "u://?\(result1)"),
let urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false) {
// ...
let t: String? = urlComponents.queryItems?["t"] // "20171017T1201"
let s: String? = urlComponents.queryItems?["s"] // "349.00"
let fp: String? = urlComponents.queryItems?["fp"] // "124519970"
let random: String? = urlComponents.queryItems?["random"] // nil
// ...
}
注意: 此解决方案基于输入字符串(查询字符串)的性质,如果输入格式将来发生变化,您可能需要直接使用正则表达式来提取信息。
答案 3 :(得分:0)
你可以像:
那样实现它if let t1 = result1.components(separatedBy: "t=")[1].components(separatedBy: "&").first {
print(t1) // 20171017T1201
}
if let fn1 = result1.components(separatedBy: "fn=")[1].components(separatedBy: "&").first {
print(fn1) // 8712000100030779
}
注意如果您非常确定您的字符串是网址参数,我建议您关注@ Milan Nosáľ answer。