No Description

NSString+YLString.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // NSString+YLString.m
  3. // YoungCatogory
  4. //
  5. // Created by kuxuan on 2017/7/12.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "NSString+YLString.h"
  9. #import <CommonCrypto/CommonDigest.h>
  10. #import "NSData+YLData.h"
  11. @implementation NSString (YLString)
  12. + (NSString *)stringWithUUID
  13. {
  14. CFUUIDRef uuidObj = CFUUIDCreate(nil);//create a new UUID
  15. //get the string representation of the UUID
  16. NSString *uuidString = (__bridge_transfer NSString *)CFUUIDCreateString(nil, uuidObj);
  17. CFRelease(uuidObj);
  18. return uuidString;
  19. }
  20. - (NSString *)md5
  21. {
  22. const char *cStr = [self UTF8String];
  23. unsigned char result [CC_MD5_DIGEST_LENGTH];
  24. CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
  25. return [NSString stringWithFormat: @"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
  26. result[0], result[1],
  27. result[2], result[3],
  28. result[4], result[5],
  29. result[6], result[7],
  30. result[8], result[9],
  31. result[10], result[11],
  32. result[12], result[13],
  33. result[14], result[15]
  34. ];
  35. }
  36. + (NSString *)cachesPath
  37. {
  38. static dispatch_once_t onceToken;
  39. static NSString *cachedPath;
  40. dispatch_once(&onceToken, ^{
  41. cachedPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
  42. });
  43. return cachedPath;
  44. }
  45. + (NSString *)documentsPath
  46. {
  47. static dispatch_once_t onceToken;
  48. static NSString *cachedPath;
  49. dispatch_once(&onceToken, ^{
  50. cachedPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  51. });
  52. return cachedPath;
  53. }
  54. #pragma mark Temporary Paths
  55. + (NSString *)temporaryPath
  56. {
  57. static dispatch_once_t onceToken;
  58. static NSString *cachedPath;
  59. dispatch_once(&onceToken, ^{
  60. cachedPath = NSTemporaryDirectory();
  61. });
  62. return cachedPath;
  63. }
  64. - (BOOL)hasString:(NSString * _Nonnull)substring {
  65. return [self hasString:substring caseSensitive:YES];
  66. }
  67. - (BOOL)hasString:(NSString *)substring caseSensitive:(BOOL)caseSensitive {
  68. if (caseSensitive) {
  69. return [self rangeOfString:substring].location != NSNotFound;
  70. } else {
  71. return [self.lowercaseString rangeOfString:substring.lowercaseString].location != NSNotFound;
  72. }
  73. }
  74. - (BOOL)isEmail {
  75. return [NSString isEmail:self];
  76. }
  77. + (BOOL)isEmail:(NSString * _Nonnull)email {
  78. NSString *emailRegEx = @"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$";
  79. NSPredicate *regExPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegEx];
  80. return [regExPredicate evaluateWithObject:[email lowercaseString]];
  81. }
  82. + (NSString * _Nonnull)convertToUTF8Entities:(NSString * _Nonnull)string {
  83. return [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[string
  84. stringByReplacingOccurrencesOfString:@"%27" withString:@"'"]
  85. stringByReplacingOccurrencesOfString:[@"%e2%80%99" capitalizedString] withString:@"’"]
  86. stringByReplacingOccurrencesOfString:[@"%2d" capitalizedString] withString:@"-"]
  87. stringByReplacingOccurrencesOfString:[@"%c2%ab" capitalizedString] withString:@"«"]
  88. stringByReplacingOccurrencesOfString:[@"%c2%bb" capitalizedString] withString:@"»"]
  89. stringByReplacingOccurrencesOfString:[@"%c3%80" capitalizedString] withString:@"À"]
  90. stringByReplacingOccurrencesOfString:[@"%c3%82" capitalizedString] withString:@"Â"]
  91. stringByReplacingOccurrencesOfString:[@"%c3%84" capitalizedString] withString:@"Ä"]
  92. stringByReplacingOccurrencesOfString:[@"%c3%86" capitalizedString] withString:@"Æ"]
  93. stringByReplacingOccurrencesOfString:[@"%c3%87" capitalizedString] withString:@"Ç"]
  94. stringByReplacingOccurrencesOfString:[@"%c3%88" capitalizedString] withString:@"È"]
  95. stringByReplacingOccurrencesOfString:[@"%c3%89" capitalizedString] withString:@"É"]
  96. stringByReplacingOccurrencesOfString:[@"%c3%8a" capitalizedString] withString:@"Ê"]
  97. stringByReplacingOccurrencesOfString:[@"%c3%8b" capitalizedString] withString:@"Ë"]
  98. stringByReplacingOccurrencesOfString:[@"%c3%8f" capitalizedString] withString:@"Ï"]
  99. stringByReplacingOccurrencesOfString:[@"%c3%91" capitalizedString] withString:@"Ñ"]
  100. stringByReplacingOccurrencesOfString:[@"%c3%94" capitalizedString] withString:@"Ô"]
  101. stringByReplacingOccurrencesOfString:[@"%c3%96" capitalizedString] withString:@"Ö"]
  102. stringByReplacingOccurrencesOfString:[@"%c3%9b" capitalizedString] withString:@"Û"]
  103. stringByReplacingOccurrencesOfString:[@"%c3%9c" capitalizedString] withString:@"Ü"]
  104. stringByReplacingOccurrencesOfString:[@"%c3%a0" capitalizedString] withString:@"à"]
  105. stringByReplacingOccurrencesOfString:[@"%c3%a2" capitalizedString] withString:@"â"]
  106. stringByReplacingOccurrencesOfString:[@"%c3%a4" capitalizedString] withString:@"ä"]
  107. stringByReplacingOccurrencesOfString:[@"%c3%a6" capitalizedString] withString:@"æ"]
  108. stringByReplacingOccurrencesOfString:[@"%c3%a7" capitalizedString] withString:@"ç"]
  109. stringByReplacingOccurrencesOfString:[@"%c3%a8" capitalizedString] withString:@"è"]
  110. stringByReplacingOccurrencesOfString:[@"%c3%a9" capitalizedString] withString:@"é"]
  111. stringByReplacingOccurrencesOfString:[@"%c3%af" capitalizedString] withString:@"ï"]
  112. stringByReplacingOccurrencesOfString:[@"%c3%b4" capitalizedString] withString:@"ô"]
  113. stringByReplacingOccurrencesOfString:[@"%c3%b6" capitalizedString] withString:@"ö"]
  114. stringByReplacingOccurrencesOfString:[@"%c3%bb" capitalizedString] withString:@"û"]
  115. stringByReplacingOccurrencesOfString:[@"%c3%bc" capitalizedString] withString:@"ü"]
  116. stringByReplacingOccurrencesOfString:[@"%c3%bf" capitalizedString] withString:@"ÿ"]
  117. stringByReplacingOccurrencesOfString:@"%20" withString:@" "];
  118. }
  119. + (NSString * _Nonnull)encodeToBase64:(NSString * _Nonnull)string {
  120. NSData *data = [string convertToNSData];
  121. return [data base64EncodedStringWithOptions:0];
  122. }
  123. - (NSString * _Nonnull)encodeToBase64 {
  124. return [NSString encodeToBase64:self];
  125. }
  126. + (NSString * _Nonnull)decodeBase64:(NSString * _Nonnull)string {
  127. NSData *data = [[NSData alloc] initWithBase64EncodedString:string options:0];
  128. return [data convertToUTF8String];
  129. }
  130. - (NSString * _Nonnull)decodeBase64 {
  131. return [NSString decodeBase64:self];
  132. }
  133. + (NSData * _Nonnull)convertToNSData:(NSString * _Nonnull)string {
  134. return [string dataUsingEncoding:NSUTF8StringEncoding];
  135. }
  136. - (NSData * _Nonnull)convertToNSData {
  137. return [NSString convertToNSData:self];
  138. }
  139. - (NSString * _Nonnull)sentenceCapitalizedString {
  140. if (![self length]) {
  141. return @"";
  142. }
  143. NSString *uppercase = [[self substringToIndex:1] uppercaseString];
  144. NSString *lowercase = [[self substringFromIndex:1] lowercaseString];
  145. return [uppercase stringByAppendingString:lowercase];
  146. }
  147. - (NSString * _Nonnull)dateFromTimestamp {
  148. NSString *year = [self substringToIndex:4];
  149. NSString *month = [[self substringFromIndex:5] substringToIndex:2];
  150. NSString *day = [[self substringFromIndex:8] substringToIndex:2];
  151. NSString *hours = [[self substringFromIndex:11] substringToIndex:2];
  152. NSString *minutes = [[self substringFromIndex:14] substringToIndex:2];
  153. return [NSString stringWithFormat:@"%@/%@/%@ %@:%@", day, month, year, hours, minutes];
  154. }
  155. - (NSString * _Nonnull)urlEncode {
  156. return [self URLEncode];
  157. }
  158. - (NSString * _Nonnull)URLEncode {
  159. return [self stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]];
  160. }
  161. - (NSString * _Nonnull)removeExtraSpaces {
  162. NSString *squashed = [self stringByReplacingOccurrencesOfString:@"[ ]+" withString:@" " options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
  163. return [squashed stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  164. }
  165. - (NSString * _Nonnull)stringByReplacingWithRegex:(NSString * _Nonnull)regexString withString:(NSString * _Nonnull)replacement {
  166. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:NSRegularExpressionCaseInsensitive error:nil];
  167. return [regex stringByReplacingMatchesInString:self options:0 range:NSMakeRange(0, [self length]) withTemplate:@""];
  168. }
  169. - (NSString * _Nonnull)HEXToString {
  170. NSMutableString *newString = [NSMutableString string];
  171. NSArray *components = [self componentsSeparatedByString:@" "];
  172. for (NSString * component in components) {
  173. int value = 0;
  174. sscanf([component cStringUsingEncoding:NSASCIIStringEncoding], "%x", &value);
  175. [newString appendFormat:@"%c", (char)value];
  176. }
  177. return newString;
  178. }
  179. - (NSString * _Nonnull)stringToHEX {
  180. NSUInteger len = [self length];
  181. unichar *chars = malloc(len * sizeof(unichar));
  182. [self getCharacters:chars];
  183. NSMutableString *hexString = [[NSMutableString alloc] init];
  184. for (NSUInteger i = 0; i < len; i++ ) {
  185. [hexString appendFormat:@"%02x", chars[i]];
  186. }
  187. free(chars);
  188. return hexString;
  189. }
  190. + (NSString * _Nonnull)generateUUID {
  191. CFUUIDRef theUUID = CFUUIDCreate(NULL);
  192. CFStringRef string = CFUUIDCreateString(NULL, theUUID);
  193. CFRelease(theUUID);
  194. return (__bridge_transfer NSString *)string;
  195. }
  196. - (BOOL)isUUID {
  197. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$" options:NSRegularExpressionCaseInsensitive error:nil];
  198. NSUInteger matches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
  199. return matches == 1;
  200. }
  201. - (BOOL)isUUIDForAPNS {
  202. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9a-f]{32}$" options:NSRegularExpressionCaseInsensitive error:nil];
  203. NSUInteger matches = [regex numberOfMatchesInString:self options:0 range:NSMakeRange(0, [self length])];
  204. return matches == 1;
  205. }
  206. - (NSString * _Nonnull)convertToAPNSUUID {
  207. return [[[self stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""] stringByReplacingOccurrencesOfString:@"-" withString:@""];
  208. }
  209. - (NSString *)trimAllWhiteSpace
  210. {
  211. return [self stringByReplacingOccurrencesOfString:@"\\s" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, self.length)];
  212. }
  213. - (CGFloat)heightForWidth:(float)width andFont:(UIFont * _Nonnull)font {
  214. CGSize size = CGSizeZero;
  215. if (self.length > 0) {
  216. CGRect frame = [self boundingRectWithSize:CGSizeMake(width, 999999) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName: font } context:nil];
  217. size = CGSizeMake(frame.size.width, frame.size.height + 1);
  218. }
  219. return size.height;
  220. }
  221. + (NSString *)getAppName{
  222. NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
  223. NSString *appName = [infoDict objectForKey:@"CFBundleDisplayName"];
  224. return appName;
  225. }
  226. @end