Iphone 检查字符串是否有空格
我有一个包含url的字符串。我试图检查url是否有无效的空格Iphone 检查字符串是否有空格,iphone,swift,Iphone,Swift,我有一个包含url的字符串。我试图检查url是否有无效的空格 let url = "http://www.example.com/images/pretty pic.png" 正如您在本例中所看到的,在pretty和pic之间有一个空格。谢谢。使用简单索引查找空间 function hasWhiteSpace(s) { return s.indexOf(' ') >= 0; } 使用simple indexOf查找空间 function hasWhiteSpace(s) { r
let url = "http://www.example.com/images/pretty pic.png"
正如您在本例中所看到的,在pretty和pic之间有一个空格。谢谢。使用简单索引查找空间
function hasWhiteSpace(s) {
return s.indexOf(' ') >= 0;
}
使用simple indexOf查找空间
function hasWhiteSpace(s) {
return s.indexOf(' ') >= 0;
}
另一种选择:检查字符串的字符集是否包含空格
let url = "http://www.example.com/images/pretty pic.png"
url.characters.contains(" ") // true
let url = "http://www.example.com/images/prettypic.png"
url.characters.contains(" ") // false
另一种选择:检查字符串的字符集是否包含空格
let url = "http://www.example.com/images/pretty pic.png"
url.characters.contains(" ") // true
let url = "http://www.example.com/images/prettypic.png"
url.characters.contains(" ") // false
在这里,返回bool的字符串扩展将是更优雅的解决方案
extension String {
public var hasWhiteSpace: Bool {
return self.contains(" ")
}
}
在这里,返回bool的字符串扩展将是更优雅的解决方案
extension String {
public var hasWhiteSpace: Bool {
return self.contains(" ")
}
}
斯威夫特5号
var string = "Hi "
if string.contains(" "){
print("Has space")
}else{
print("Does not have space")
}
斯威夫特5号
var string = "Hi "
if string.contains(" "){
print("Has space")
}else{
print("Does not have space")
}
使用containsstringther:String->Bool会更有意义,因为您没有使用范围。使用containsstringther:String->Bool会更有意义,因为您没有使用范围。错误使用未声明的类型错误使用未声明的类型