2017-06-20 22 views
0

我是新来的swift。我需要得到的单词列表形式是一个字符串,其前缀为#。这里是我的字符串获取字符串中的单词列表

“三十人是在6月14日发生火灾后确认死亡,但总预期上升得多。......一个人谁贴的图片上#Facebook有人认为身体已跃居从#Grenfell塔#fire他去世已经#jailed三个月”

我需要流动词的列表:

["#Facebook","#Grenfell","#fire","#jailed"] 

我花的时间很多,但无法弄清楚如何实现这一点。

+0

的可能的复制过滤字符串[夫特:分割字符串到一个数组(https://stackoverflow.com/questions/25678373/ swift-split-a-string-into-an-an-array) – Lepidopteron

回答

2

你可以做到这一点是:

let str = "Thirty people are confirmed dead after the June 14 fire, but the total is expected to rise far higher. ... A man who posted pictures on #Facebook of the body of someone believed to have leapt to his death from the #Grenfell Tower #fire has been #jailed for three months" 

let words = str.components(separatedBy: " ").filter { (element) -> Bool in 
     return element.hasPrefix("#") 
    } 
print(words) 
0

下面是简单的解决方案:

逻辑:拆分文本字的数组,然后用谓词过滤。

let stringText = "Thirty people are confirmed dead after the June 14 fire, but the total is expected to rise far higher. ... A man who posted pictures on #Facebook of the body of someone believed to have leapt to his death from the #Grenfell Tower #fire has been #jailed for three months" 

    let wordsArray = stringText.characters.split{$0 == " "}.map(String.init) 

    let filteredList = wordsArray.filter {NSPredicate(format:"SELF BEGINSWITH %@","#").evaluate(with: $0)} 
0

使用NSRegularExpression用于与某些前缀原样

//Xcode 8.3 swift 3.x 
    var string: String = "Thirty people are confirmed dead after the June 14 fire, but the total is expected to rise far higher. ... A man who posted pictures on #Facebook of the body of someone believed to have leapt to his death from the #Grenfell Tower #fire has been #jailed for three months" 

let regex = try? NSRegularExpression(pattern: "#(\\w+)", options: []) 
let matches: [NSTextCheckingResult] = (regex?.matches(in: string, options: [], range: NSRange(location: 0, length: (string.characters.count))))! 
for match in matches { 
    let wordRange = (match).rangeAt(1) 
    let word = (string as NSString).substring(with: wordRange) 
    print("Found tag - \(word)") 
} 




    // Objective c -> 

NSString*string = @"Thirty people are confirmed dead after the June 14 fire, but the total is expected to rise far higher. ... A man who posted pictures on #Facebook of the body of someone believed to have leapt to his death from the #Grenfell Tower #fire has been #jailed for three months"; 
    NSError *error = nil; 

    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"#(\\w+)" options:0 error:&error]; 
    NSArray *matches = [regex matchesInString:string options:0 range:NSMakeRange(0, string.length)]; 
    for (NSTextCheckingResult *match in matches) { 
     NSRange wordRange = [match rangeAtIndex:1]; 
     NSString* word = [string substringWithRange:wordRange]; 
     NSLog(@"Found tag %@", word); 
    } 
+0

不要注释编译器可以推断的类型。 ''NSTextCheckingResult''不是'[Any]''的结果类型是''NSTextCheckingResult'',你会让事情变得更糟。 – vadian

+0

@vadian多数民众赞成好听,我编辑答案。 – Jack

+0

现在你也可以删除丑陋的'as AnyObject '类型转换,因为类型是不同的。请移除所有其他类型的注释。 – vadian

相关问题