String.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (C) 2016 Inigo Valentin
  2. //
  3. // This file is part of the Gasteizko Margolariak IOS app.
  4. //
  5. // The Gasteizko Margolariak IOS app is free software: you can
  6. // redistribute it and/or modify it under the terms of the
  7. // GNU General Public License as published by the Free Software
  8. // Foundation, either version 3 of the License, or (at your
  9. // option) any later version.
  10. //
  11. // The Gasteizko Margolariak IOS app is distributed in the
  12. // hope that it will be useful, but WITHOUT ANY WARRANTY;
  13. // without even the implied warranty of MERCHANTABILITY or
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  15. // Public License for more details.
  16. //
  17. // You should have received a copy of the GNU General Public
  18. // License along with the Gasteizko Margolariak IOS app.
  19. // If not, see <http://www.gnu.org/licenses/>.
  20. import Foundation
  21. import UIKit
  22. /**
  23. Extension of String to include custom utilities.
  24. */
  25. extension String {
  26. /**
  27. The length of the string.
  28. */
  29. var length:Int {
  30. return self.characters.count
  31. }
  32. /**
  33. Finds the first position of a substring.
  34. :param: target The substring to look for.
  35. :return: The index of the first occurrence of the substring.
  36. */
  37. func indexOf(target: String) -> Int? {
  38. let range = (self as NSString).range(of: target)
  39. guard range.toRange() != nil else {
  40. return nil
  41. }
  42. return range.location
  43. }
  44. /**
  45. Finds the last position of a substring.
  46. :param: target The substring to look for.
  47. :return: The index of the las ocurrence of the substring.
  48. */
  49. func lastIndexOf(target: String) -> Int? {
  50. let range = (self as NSString).range(of: target, options: NSString.CompareOptions.backwards)
  51. guard range.toRange() != nil else {
  52. return nil
  53. }
  54. return self.length - range.location - 1
  55. }
  56. /**
  57. Checks for a substring
  58. :param: target Ths substring to look for.
  59. :return: True if the substring is contained in the string, false otherwise.
  60. */
  61. func contains(s: String) -> Bool {
  62. return (self.range(of: s) != nil) ? true : false
  63. }
  64. /**
  65. Gets a subset of the string.
  66. :param: start The beggining index of the substring.
  67. :param: start The ending index of the substring.
  68. :return: The substring.
  69. */
  70. func subStr(start: Int, end: Int) -> String{
  71. let st = self.index(self.startIndex, offsetBy: start)
  72. let ed = self.index(self.startIndex, offsetBy: end)
  73. let range = st...ed
  74. return String(self[range])
  75. }
  76. /**
  77. Removes HTML symbolf from the string. It also decodes HTML symbols.
  78. :return: The string eithout the HTML symbols.
  79. */
  80. func stripHtml() -> String {
  81. let d = data(using: String.Encoding.utf8)
  82. do{
  83. return try NSAttributedString(data: d!, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html, NSAttributedString.DocumentReadingOptionKey.characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil).string
  84. } catch let error as NSError {
  85. print(error.localizedDescription)
  86. return self
  87. }
  88. }
  89. /**
  90. Removes escaped characters and unicode symbols from the string.
  91. :return: Escaped string.
  92. */
  93. func decode() -> String {
  94. let transform: NSString = "Any-Hex/Java"
  95. let convertedString: NSMutableString = (self as NSString).mutableCopy() as! NSMutableString
  96. CFStringTransform(convertedString, nil, transform, true)
  97. return (convertedString as String).replacingOccurrences(of: "\\/", with: "/")
  98. }
  99. /**
  100. Capitalizes a string, turning the first caracter to uppercase.
  101. :return: Capitalized string.
  102. */
  103. func capitalize() -> String {
  104. let first = String(characters.prefix(1)).capitalized
  105. let other = String(characters.dropFirst())
  106. return first + other
  107. }
  108. }