我在swift中的文本配置文件中读取并需要从ip地址中提取数字和小数。 config.txt文件包含line- ip地址:192.0.168.1
到目前为止,我有 -
let path = "/config.txt"
do {
// Use contentsOfFile overload.
// ... Specify ASCII encoding.
// ... Ignore errors.
let data = try NSString(contentsOfFile: path, encoding: NSASCIIStringEncoding)
// If a value was returned, print it.
let str = data
let intString = str.componentsSeparatedByCharactersInSet(NSCharacterSet
.decimalDigitCharacterSet()
.invertedSet)
.joinWithSeparator("")
print(data)
print(intString)
} catch {
}
当然这只会返回ip地址的编号。我很难得到正确的格式192.0.168.1
答案 0 :(得分:0)
如果总是在IP地址前面显示相同的"ip address: "
文字,那么它非常简单:
let str = "ip address: 192.0.168.1"
if let intString = str.componentsSeparatedByString("address: ").last {
print(intString)
}
打印:
192.0.168.1