Advertisement
Guest User

Untitled

a guest
Nov 17th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 6.31 KB | None | 0 0
  1. import Foundation
  2. import UIKit
  3.  
  4.  
  5. // these are the "parts" we want to extract from the string
  6. var calendarName = ""
  7. var dateTimeParts = ""
  8. var locationName = ""
  9.  
  10. var index = -1
  11.  
  12. var str = "Today 11:55 AM Tony plays pickleball weekly on court 1 calendar Tony repeating weekly location court 1"
  13.  
  14. var wordArrOriginal = str.components(separatedBy: " ")      // "original" array without change to lower-case
  15. var wordArrTrimmed = str.lowercased().components(separatedBy: " ")
  16.  
  17. print(#line, "wordArrayOriginal:", wordArrOriginal)
  18. print(#line, "wordArrayTrimmed: ", wordArrTrimmed)
  19.  
  20. print() // print a blank line
  21.  
  22.  
  23. // now let's look for Repeating
  24. // 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
  25.  
  26. //index = wordArrTrimmed.index(of: "repeating") ?? -1
  27.  
  28. if index == -1 {
  29.     index = wordArrTrimmed.index(of: "repeat") ?? -1
  30. } else if index == -1 {
  31.     index = wordArrTrimmed.index(of: "repeating") ?? -1
  32. } else if index == -1  {
  33.     index = wordArrTrimmed.index(of: "reoccurring") ?? -1
  34. }
  35.  
  36. print(#line, "index of location:", index)
  37.  
  38. print()
  39.  
  40. print(#line, "wordArrTrimmed.count:", wordArrTrimmed.count)
  41.  
  42.  
  43. if index > -1 {     // we found a repeating type word
  44.    
  45.     if index < wordArrTrimmed.count - 1 {   // this tells us there is at least 1 more word after "repeat"
  46.        
  47.         // so, assign the rest of the string (the rest of the array elements) to our locationName var
  48.        
  49.         locationName = wordArrOriginal[(index + 1)..<wordArrOriginal.count].joined(separator: " ")
  50.        
  51.         // and remove those words (plus the "repeat" keyword) from the array
  52.        
  53.         wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
  54.         wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
  55.        
  56.     } else {    // "Repeat" was the LAST word, so just remove "Location" (the last element) from the array
  57.        
  58.         wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
  59.         wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
  60.        
  61.     }
  62.    
  63. } // end if repeat type word was found
  64.  
  65.  
  66. // let's look for Calendar
  67. // 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
  68.  
  69. index = wordArrTrimmed.index(of: "calendar") ?? -1
  70.  
  71. print(#line, "index of calendar:", index)
  72.  
  73. print()
  74.  
  75. if index > -1 {     // we found the word "calendar"
  76.    
  77.     if index < wordArrTrimmed.count - 1 {   // this tells us there is at least 1 more word after "calendar"
  78.        
  79.         // so, assign the NEXT word to our calendarName var
  80.        
  81.         calendarName = wordArrOriginal[index + 1]
  82.        
  83.         // and remove that word (plus the precending "calendar" keyword) from BOTH arrays
  84.        
  85.         wordArrOriginal.removeSubrange(index...index + 1)
  86.         wordArrTrimmed.removeSubrange(index...index + 1)
  87.        
  88.     } else {    // "Calendar" was the LAST word, so just remove "Calendar" (the last element) from BOTH arrays
  89.        
  90.         wordArrOriginal.remove(at: index)
  91.         wordArrTrimmed.remove(at: index)
  92.        
  93.     }
  94.    
  95. } // end if "calendar" was found
  96.  
  97. print(#line, "wordArrayOriginal:", wordArrOriginal)
  98. print(#line, "wordArrayTrimmed: ", wordArrTrimmed)
  99.  
  100. print()
  101.  
  102.  
  103. // now let's look for Location
  104. // 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
  105.  
  106. index = wordArrTrimmed.index(of: "location") ?? -1
  107.  
  108. print(#line, "index of location:", index)
  109.  
  110. print(#line, "wordArrTrimmed.count:", wordArrTrimmed.count)
  111.  
  112.  
  113. print()
  114.  
  115. if index > -1 {     // we found the word "location"
  116.    
  117.     if index < wordArrTrimmed.count - 1 {   // this tells us there is at least 1 more word after "location"
  118.        
  119.         // so, assign the rest of the string (the rest of the array elements) to our locationName var
  120.        
  121.         locationName = wordArrOriginal[(index + 1)..<wordArrOriginal.count].joined(separator: " ")
  122.        
  123.         // and remove those words (plus the "location" keyword) from the array
  124.        
  125.         wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
  126.         wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
  127.        
  128.     } else {    // "Location" was the LAST word, so just remove "Location" (the last element) from the array
  129.        
  130.         wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
  131.         wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
  132.        
  133.     }
  134.    
  135. } // end if "location" was found
  136.  
  137.  
  138. // now let's look for Repeating
  139. // 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
  140.  
  141. if index == -1 {
  142.     index = wordArrTrimmed.index(of: "repeat") ?? -1
  143. } else if index == -1 {
  144.     index = wordArrTrimmed.index(of: "repeating") ?? -1
  145. } else if index == -1  {
  146.     index = wordArrTrimmed.index(of: "reoccurring") ?? -1
  147. }
  148.  
  149. print(#line, "index of location:", index)
  150.  
  151. print()
  152.  
  153. print(#line, "wordArrTrimmed.count:", wordArrTrimmed.count)
  154.  
  155.  
  156. if index > -1 {     // we found a repeating type word
  157.    
  158.     if index < wordArrTrimmed.count - 1 {   // this tells us there is at least 1 more word after "repeat"
  159.        
  160.         // so, assign the rest of the string (the rest of the array elements) to our locationName var
  161.        
  162.         locationName = wordArrOriginal[(index + 1)..<wordArrOriginal.count].joined(separator: " ")
  163.        
  164.         // and remove those words (plus the "repeat" keyword) from the array
  165.        
  166.         wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
  167.         wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
  168.        
  169.     } else {    // "Repeat" was the LAST word, so just remove "Location" (the last element) from the array
  170.        
  171.         wordArrOriginal.removeSubrange(index..<wordArrOriginal.count)
  172.         wordArrTrimmed.removeSubrange(index..<wordArrTrimmed.count)
  173.        
  174.     }
  175.    
  176. } // end if repeat type word was found
  177.  
  178.  
  179. print(#line, "wordArrayOriginal:", wordArrOriginal)
  180. print(#line, "wordArrayTrimmed: ", wordArrTrimmed)
  181.  
  182. print()
  183.  
  184. print(#line, "calendarName:", calendarName)
  185. print(#line, "locationName:", locationName)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement