【问题标题】:Getting a list of words from a String从字符串中获取单词列表
【发布时间】:2017-06-20 03:23:18
【问题描述】:

我是 swift 新手。我需要从前缀为 # 的字符串中获取单词列表。这是我的字符串

“在 6 月 14 日的大火之后,有 30 人被确认死亡,但预计死亡人数还会更高。...一名男子在 #Facebook 上发布了据信有人尸体的照片从#Grenfell Tower 跳楼身亡#fire 已#jailed 三个月”

我需要流动的单词列表:

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

我花了很多时间,但不知道如何做到这一点。

【问题讨论】:

标签: ios arrays string swift3


【解决方案1】:

你可以这样做:

 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)

【讨论】:

    【解决方案2】:

    这是一个简单的解决方案:

    逻辑:将文本拆分为单词数组,然后使用谓词对其进行过滤。

        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)}
    

    【讨论】:

      【解决方案3】:

      使用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);
          }
      

      【讨论】:

      • 不要注释编译器可以推断的类型。 matches(in 的结果类型是 [NSTextCheckingResult] 而不是 [Any],你让它变得更糟。
      • 现在您还可以删除丑陋的as AnyObject 类型转换,因为类型是不同的。并且请删除所有其他类型注释。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多