Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Foundation
- import UIKit
- // these are the "parts" we want to extract from the string
- var calendarName = ""
- var dateTimeParts = ""
- var locationName = ""
- var index = -1
- var str = "Today 11:55 AM Tony plays pickleball weekly on court 1 calendar Tony repeating weekly location court 1"
- var wordArrOriginal = str.components(separatedBy: " ") // "original" array without change to lower-case
- var wordArrTrimmed = str.lowercased().components(separatedBy: " ")
- print(#line, "wordArrayOriginal:", wordArrOriginal)
- print(#line, "wordArrayTrimmed: ", wordArrTrimmed)
- print() // print a blank line
- // now let's look for Repeating
- // if we find it, we want to get the NEXT word as the repeating frequency name, and remove it plus the precending "repeat" keyword from BOTH arrays
- //index = wordArrTrimmed.index(of: "repeating") ?? -1
- if index == -1 {
- index = wordArrTrimmed.index(of: "repeat") ?? -1
- } else if index == -1 {
- index = wordArrTrimmed.index(of: "repeating") ?? -1
- } else if index == -1 {
- index = wordArrTrimmed.index(of: "reoccurring") ?? -1
- }
- print(#line, "index of location:", index)
- print()
- print(#line, "wordArrTrimmed.count:", wordArrTrimmed.count)
- if index > -1 { // we found a repeating type word
- if index < wordArrTrimmed.count - 1 { // this tells us there is at least 1 more word after "repeat"
- // so, assign the rest of the string (the rest of the array elements) to our locationName var
- locationName = wordArrOriginal[(index + 1)..<wordArrOriginal.count].joined(separator: " ")
- // and remove those words (plus the "repeat" keyword) from the array
- wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
- wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
- } else { // "Repeat" was the LAST word, so just remove "Location" (the last element) from the array
- wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
- wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
- }
- } // end if repeat type word was found
- // let's look for Calendar
- // if we find it, we want to get the NEXT word as the calendar name, and remove it plus the precending "calendar" keyword from BOTH arrays
- index = wordArrTrimmed.index(of: "calendar") ?? -1
- print(#line, "index of calendar:", index)
- print()
- if index > -1 { // we found the word "calendar"
- if index < wordArrTrimmed.count - 1 { // this tells us there is at least 1 more word after "calendar"
- // so, assign the NEXT word to our calendarName var
- calendarName = wordArrOriginal[index + 1]
- // and remove that word (plus the precending "calendar" keyword) from BOTH arrays
- wordArrOriginal.removeSubrange(index...index + 1)
- wordArrTrimmed.removeSubrange(index...index + 1)
- } else { // "Calendar" was the LAST word, so just remove "Calendar" (the last element) from BOTH arrays
- wordArrOriginal.remove(at: index)
- wordArrTrimmed.remove(at: index)
- }
- } // end if "calendar" was found
- print(#line, "wordArrayOriginal:", wordArrOriginal)
- print(#line, "wordArrayTrimmed: ", wordArrTrimmed)
- print()
- // now let's look for Location
- // if we find it, we want to get the REST of the string as the location name, and remove that word (those words) plus the precending "location" keyword) from BOTH arrays
- index = wordArrTrimmed.index(of: "location") ?? -1
- print(#line, "index of location:", index)
- print(#line, "wordArrTrimmed.count:", wordArrTrimmed.count)
- print()
- if index > -1 { // we found the word "location"
- if index < wordArrTrimmed.count - 1 { // this tells us there is at least 1 more word after "location"
- // so, assign the rest of the string (the rest of the array elements) to our locationName var
- locationName = wordArrOriginal[(index + 1)..<wordArrOriginal.count].joined(separator: " ")
- // and remove those words (plus the "location" keyword) from the array
- wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
- wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
- } else { // "Location" was the LAST word, so just remove "Location" (the last element) from the array
- wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
- wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
- }
- } // end if "location" was found
- // now let's look for Repeating
- // if we find it, we want to get the NEXT word as the repeating frequency name, and remove it plus the precending "repeat" keyword from BOTH arrays
- if index == -1 {
- index = wordArrTrimmed.index(of: "repeat") ?? -1
- } else if index == -1 {
- index = wordArrTrimmed.index(of: "repeating") ?? -1
- } else if index == -1 {
- index = wordArrTrimmed.index(of: "reoccurring") ?? -1
- }
- print(#line, "index of location:", index)
- print()
- print(#line, "wordArrTrimmed.count:", wordArrTrimmed.count)
- if index > -1 { // we found a repeating type word
- if index < wordArrTrimmed.count - 1 { // this tells us there is at least 1 more word after "repeat"
- // so, assign the rest of the string (the rest of the array elements) to our locationName var
- locationName = wordArrOriginal[(index + 1)..<wordArrOriginal.count].joined(separator: " ")
- // and remove those words (plus the "repeat" keyword) from the array
- wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
- wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
- } else { // "Repeat" was the LAST word, so just remove "Location" (the last element) from the array
- wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
- wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
- }
- } // end if repeat type word was found
- print(#line, "wordArrayOriginal:", wordArrOriginal)
- print(#line, "wordArrayTrimmed: ", wordArrTrimmed)
- print()
- print(#line, "calendarName:", calendarName)
- print(#line, "locationName:", locationName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement