kis*_*011 3 replace substring swift swift3
我有一个字符串和子字符串(http),我想替换该子字符串,但我不知道该子字符串何时结束.我的意思是想要检查它,直到一个空间没有来,之后我想要更换它.我正在检查如果我的字符串包含http也是一个字符串,那么我想在空间到来时替换它.
以下是我的例子: -
let string = "Hello.World everything is good http://www.google.com By the way its good".
Run Code Online (Sandbox Code Playgroud)
这是我的字符串它可以是动态的,我的意思是在上面的字符串http就在那里,所以我想将" http://www.google.com " 替换为"网站".所以它会
string = "Hello.World everything is good website By the way its good"
Run Code Online (Sandbox Code Playgroud)
可能的解决方案是正则表达式
该模式搜索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)
Run Code Online (Sandbox Code Playgroud)