如何用swift 3中的链接(http)替换子字符串?

前端之家收集整理的这篇文章主要介绍了如何用swift 3中的链接(http)替换子字符串?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个字符串和子字符串(http),我想替换该子字符串,但我不知道该子字符串何时结束.我的意思是想要检查它,直到一个空间没有来,之后我想要更换它.
我正在检查如果我的字符串包含http也是一个字符串,那么我想在空间到来时替换它.

以下是我的例子: –

let string = "Hello.World everything is good http://www.google.com By the way its good".

这是我的字符串它可以是动态的,我的意思是在上面的字符串http就在那里,所以我想将“http://www.google.com”替换为“网站”.
所以它会

string = "Hello.World everything is good website By the way its good"

解决方法

可能的解决方案是正则表达式

该模式搜索http://或https://后跟一个或多个非空白字符,直到字边界.

let string = "Hello.World everything is good http://www.google.com By the way its good"
let trimmedString = string.replacingOccurrences(of: "https?://\\S+\\b",with: "website",options: .regularExpression)
print(trimmedString)

猜你在找的Swift相关文章