gpt4 book ai didi

Swift:从字符串中提取子字符串

转载 作者:行者123 更新时间:2023-12-04 09:01:22 29 4
gpt4 key购买 nike

我有一个字符串,它可以是以下之一:

tempString = "What? The ID: 54673 Over there"
tempString = "Jump... ID: 4561E how high"
基本上,我想从字符串中检索五个字符的 ID:。对于大多数语言,我可以通过查找 ID: 来实现这一点,向其添加 4 以到达 ID: 的开头,然后获取接下来的五个字符。
例如,在 Excel 中:
=MID("Jump... ID: 4561E how high",FIND("ID:","Jump... ID: 4561E how high")+4, 5)
将 = 4561E。
我们如何用 Swift 做到这一点?

最佳答案

在“ID:”之后从 [0-9] 或 [A-F] 中查找前 5 个字符的正则表达式方法(仅当您不能使用 A-Z 时才使用十六进制字母)。

if let range = string.range(of: "(?<=ID: )[0-9A-F]{5}", options: .regularExpression) {
let id = string[range] //
// if you need a String instead of a substring
let stringID = String(string[range])
}

编辑/更新:
看看你的答案,看起来要求与你的原始帖子完全不同,无论如何找到一个 IF 复合 8 个十六进制字符后跟连字符然后 4 个六进制字符后跟连字符(3 次)比 12 个十六进制字符你可以使用以下正则表达式 "([0-9a-f]{8}-)([0-9a-f]{4}-){3}([0-9a-f]{12})" :
let dataString = """
{
"@odata.context": "$metadata#GeoFences(Points())/$entity",
"ID": "2b2a2abc-5962-4290-92b4-773025ffd50b",
"Points": {
"POINT_TYPE": "F",
"POINT_NUM": 0,
"LATITUDE": 32.92197686725423,
"LONGITUDE": -117.04306932263532,
"parent_ID": "2b2a2abc-5962-4290-92b4-773025ffd50b"
},
"GEOFENCE_NAME": "New Fence",
"GEOFENCE_TYPE": "O",
"PRIVACY": "X",
"CENTER_LAT": 32.92043316309709,
"CENTER_LONG": -117.04286922250975,
"ZOOM_LAT": 0.006238797350533787,
"ZOOM_LONG": 0.005345531926053582,
"PATH_TOLERANCE": 5,
"ENTRANCE_TOLERANCE": 5
}
"""
if let range = dataString.range(of: "([0-9a-f]{8}-)([0-9a-f]{4}-){3}([0-9a-f]{12})", options: .regularExpression) {
let id = dataString[range] // 4561E
print("ID:", id)
// if you need a String instead of a substring
let stringID = String(dataString[range])
print("stringID:", stringID)
}

这将打印

ID: 2b2a2abc-5962-4290-92b4-773025ffd50b

stringID: 2b2a2abc-5962-4290-92b4-773025ffd50b



请注意,您的代码将导致 "2b2a2abc-5962-4290-92b4-773025ffd50
编辑/更新2:
考虑到您的字符串是 JSON,您可以简单地解码您的字符串 ID:
struct Root: Codable {
let id: String
enum CodingKeys: String, CodingKey {
case id = "ID"
}
}
do {
let id = try JSONDecoder().decode(Root.self, from: Data(dataString.utf8)).id
print(id) // "2b2a2abc-5962-4290-92b4-773025ffd50b"
} catch {
print(error)
}

如果您需要解码所有数据:
struct Root: Codable {
let odataContext, id: String
let points: Points
let geofenceName, geofenceType, privacy: String
let centerLat, centerLong, zoomLat, zoomLong: Double
let pathTolerance, entranceTolerance: Int

enum CodingKeys: String, CodingKey {
case odataContext = "@odata.context", id = "ID", points = "Points", geofenceName = "GEOFENCE_NAME", geofenceType = "GEOFENCE_TYPE", privacy = "PRIVACY", centerLat = "CENTER_LAT", centerLong = "CENTER_LONG", zoomLat = "ZOOM_LAT", zoomLong = "ZOOM_LONG", pathTolerance = "PATH_TOLERANCE", entranceTolerance = "ENTRANCE_TOLERANCE"
}
}
struct Points: Codable {
let pointType: String
let pointNum: Int
let latitude, longitude: Double
let parentID: String

enum CodingKeys: String, CodingKey {
case pointType = "POINT_TYPE", pointNum = "POINT_NUM", latitude = "LATITUDE", longitude = "LONGITUDE", parentID = "parent_ID"
}
}
do {
let root = try JSONDecoder().decode(Root.self, from: Data(dataString.utf8))
print("ID:", root.id) // ID: 2b2a2abc-5962-4290-92b4-773025ffd50b
print("Root:", root) // Root: Root(odataContext: "$metadata#GeoFences(Points())/$entity", id: "2b2a2abc-5962-4290-92b4-773025ffd50b", points: __lldb_expr_111.Points(pointType: "F", pointNum: 0, latitude: 32.92197686725423, longitude: -117.04306932263532, parentID: "2b2a2abc-5962-4290-92b4-773025ffd50b"), geofenceName: "New Fence", geofenceType: "O", privacy: "X", centerLat: 32.92043316309709, centerLong: -117.04286922250975, zoomLat: 0.0062387973505337885, zoomLong: 0.005345531926053582, pathTolerance: 5, entranceTolerance: 5)
} catch {
print(error)
}

关于Swift:从字符串中提取子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63551808/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com