-1

If i have a string something like: "Hello world (this is Sam)" i need to get the following array: ["Hello world", "this is Sam"] and the following ["Hello","World","this is Sam"] What would be the best way to achieve this in Swift?

Sam Bing
  • 2,794
  • 1
  • 19
  • 29

3 Answers3

1

Not sure if you still need this but you can try this.

let originalString = "Hello world (this is Sam) Hello world (this is Sam) (this is Sam) Hello world Hello world (this is Sam)"
let newArr = originalString.components(separatedBy: ["(", ")"])

var finalArr = [String]()

for (index, value) in newArr.enumerated() {

    if (index + 1) % 2 == 1 {
        finalArr.append(contentsOf: value.components(separatedBy: " ").filter { $0 != "" })
    }
    else {
        finalArr.append(value)
    }
}

print(finalArr) //["Hello", "world", "this is Sam", "Hello", "world", "this is Sam", "this is Sam", "Hello", "world", "Hello", "world", "this is Sam"]
koropok
  • 1,393
  • 13
  • 19
0

Concerning the first case, you could just split the string using parentheses as separators. Then, omittingEmptySubsequences will remove any empty string that goes into the split. Finally, trim any whitespaces or new lines that were left on any of the splitted elements.

let splittedText = text.split(omittingEmptySubsequences: true) { separator in
                       return separator == "(" || separator == ")"
                   }.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
Oxthor
  • 512
  • 4
  • 15
  • Thank you for the answer both seem to achieve the same thing, would you also regard my comment above about getting a result like ["Hello","World","this is Sam"] – Sam Bing Dec 26 '17 at 08:45
0

Here is the way you can achieve that:

let originalString = "Hello world (this is Sam)"
let newArr = originalString.components(separatedBy: ["(", ")"]).filter { $0 != "" }
print(newArr) //["Hello world ", "this is Sam"]

I have mixed this and this post to achieve it.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
  • You should also avoid whitespaces when splitting, as it is shown in the desired result. – Oxthor Dec 26 '17 at 08:42
  • Thank you for the answer, it works.Also what would it be required to split it like this : ["Hello","World","this is Sam"] – Sam Bing Dec 26 '17 at 08:43
  • You can do that by taking first object from the above result array and again split it with space like `separatedBy: [" "]` – Dharmesh Kheni Dec 26 '17 at 08:46
  • What if the array had more text ? That would still just separate the first object in the array – Sam Bing Dec 26 '17 at 08:48
  • What is your exact requirement because what you have asked in question is different than what you are discussing here – Dharmesh Kheni Dec 26 '17 at 08:50
  • Turns out i need both and now i know how to achieve one of the desired results. If i also want to split the string string like "Some long string (don't separate this) some more text" i would need it to be split like the following:["Some","long","string","don't separate this","some","more","text"] – Sam Bing Dec 26 '17 at 09:02
  • You should mentioned all your requirement from start in your question. Please update your question with your requirements. Thanks. – Dharmesh Kheni Dec 26 '17 at 09:03