0

i'm new to swift . i need to get list of word form an string that has prefix with # . here is my 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"

i need flowing list of word :

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

I spent lot of time but could not figure out how to achieve this.

nayem
  • 7,285
  • 1
  • 33
  • 51
cristainlika3
  • 279
  • 1
  • 4
  • 15
  • Possible duplicate of [Swift: Split a String into an array](https://stackoverflow.com/questions/25678373/swift-split-a-string-into-an-array) – Lepidopteron Jun 20 '17 at 06:09

3 Answers3

2

You can do this as:

 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

Here is the simple solution :

Logic : Split text into array of words and then filter it with predicate.

    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)}
Vinit Ingale
  • 381
  • 1
  • 4
  • 17
0

Use NSRegularExpression for filtering the string with certain prefix as-

    //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);
    }
Jack
  • 13,571
  • 6
  • 76
  • 98
  • Don't annotate types the compiler can infer. The result type of `matches(in` is `[NSTextCheckingResult]` not `[Any]`, you make it worse. – vadian Jun 20 '17 at 06:41
  • Now you can also remove the ugly `as AnyObject` type cast, since the type is distinct. And please remove all other type annotations, too. – vadian Jun 20 '17 at 07:18