TextUtils.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // TextUtils.m
  3. // AntsContract
  4. //
  5. // Created by Ray on 12/20/16.
  6. // Copyright © 2016 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "TextUtils.h"
  9. @implementation TextUtils
  10. + (long)countOccurencesOfString:(NSString*)string find:(NSString*)searchString {
  11. long strCount = string.length - [[string stringByReplacingOccurrencesOfString:searchString withString:@""] length];
  12. return strCount / [searchString length];
  13. }
  14. + (NSString *)formatNumber:(NSString *)mobileNumber
  15. {
  16. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
  17. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
  18. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
  19. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
  20. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
  21. NSLog(@"%@", mobileNumber);
  22. int length = (int)[mobileNumber length];
  23. if(length > 10)
  24. {
  25. mobileNumber = [mobileNumber substringFromIndex: length-10];
  26. NSLog(@"%@", mobileNumber);
  27. }
  28. return mobileNumber;
  29. }
  30. + (int)getLength:(NSString *)mobileNumber
  31. {
  32. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
  33. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
  34. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
  35. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
  36. mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
  37. int length = (int)[mobileNumber length];
  38. return length;
  39. }
  40. +(NSString*) encodePhoneNumber:(NSString*) phone
  41. {
  42. NSLocale* locale = [NSLocale currentLocale];
  43. if( [[locale localeIdentifier] compare:@"en_US"]!=NSOrderedSame )
  44. return phone;
  45. if( [phone length]==0 )
  46. return phone;
  47. if( [phone rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] ].location==NSNotFound )
  48. {
  49. const char* string = [phone UTF8String];
  50. long length = [phone lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
  51. [[NSString stringWithCString:string encoding:NSUTF8StringEncoding] substringToIndex:1];
  52. // if( [[NSString stringWithCString:string length:1] intValue]==1 )
  53. if([[[NSString stringWithCString:string encoding:NSUTF8StringEncoding] substringToIndex:1] intValue]==1)
  54. {
  55. if( length > 11 )
  56. return phone;
  57. NSMutableData* array = [[NSMutableData alloc] init] ;
  58. // 1 (234) 567-8901
  59. for( int i=0; i < length; i++ )
  60. {
  61. if( i==1 )
  62. {
  63. [array appendBytes:" (" length:2];
  64. }
  65. if( i == 4 )
  66. {
  67. [array appendBytes:") " length:2];
  68. }
  69. if( i==7 )
  70. {
  71. [array appendBytes:" " length:1];
  72. }
  73. [array appendBytes:string++ length:1];
  74. }
  75. return [NSString stringWithUTF8String:(const char*)[array bytes]];
  76. }
  77. else
  78. {
  79. // (012) 345-6789
  80. if( length > 10 )
  81. return phone;
  82. NSMutableData* array = [[NSMutableData alloc] init] ;
  83. int i=0;
  84. if( length <=7 )
  85. {
  86. for( i=0; i < length; i++ )
  87. {
  88. if( i==3 )
  89. {
  90. [array appendBytes:"-" length:1];
  91. }
  92. [array appendBytes:string++ length:1];
  93. }
  94. }
  95. else
  96. {
  97. for( i=0; i < length; i++ )
  98. {
  99. if( i==0 )
  100. [array appendBytes:"(" length:1];
  101. if( i==3 )
  102. [array appendBytes:") " length:2];
  103. if( i==6 )
  104. [array appendBytes:"-" length:1];
  105. [array appendBytes:string++ length:1];
  106. }
  107. }
  108. [array appendBytes:"\0" length:1];
  109. return [NSString stringWithUTF8String:(const char*)[array bytes]];
  110. }
  111. }
  112. return phone;
  113. }
  114. +(NSString*) RegularExpReplace:(NSString*) content from:(NSString*) pattern to:(NSString*) to_pattern
  115. {
  116. if(content==nil)
  117. return nil;
  118. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
  119. NSString* sss = [regex stringByReplacingMatchesInString:content options:0 range:NSMakeRange(0, content.length) withTemplate:to_pattern];
  120. return sss;
  121. }
  122. +(NSArray*) expression_varable:(NSString*)content regex:(NSString*) pattern
  123. {
  124. if(content==nil)
  125. return nil;
  126. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
  127. NSArray *matches = [regex matchesInString:content options:0 range:NSMakeRange(0, content.length)];
  128. if (matches) {
  129. for (NSTextCheckingResult *match in matches) {
  130. for (int i = 0; i < match.numberOfRanges; ++i) {
  131. DebugLog(@"-> %@", [content substringWithRange:[match rangeAtIndex:i]]);
  132. }
  133. }
  134. }
  135. return matches;
  136. }
  137. +(NSTextCheckingResult*) expression_findfistMatch:(NSString*)content regex:(NSString*) pattern
  138. {
  139. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
  140. NSTextCheckingResult *match = [regex firstMatchInString:content options:0 range:NSMakeRange(0, content.length)];
  141. return match;
  142. // if (matches) {
  143. // for (NSTextCheckingResult *match in matches) {
  144. // for (int i = 0; i < match.numberOfRanges; ++i) {
  145. // DebugLog(@"-> %@", [content substringWithRange:[match rangeAtIndex:i]]);
  146. // }
  147. // }
  148. // }
  149. // return matches;
  150. }
  151. + (CGRect)rectVAlign:(CGRect )parent rect:(CGRect)rect vAlign:(NSString*)vAlign
  152. {
  153. // double cx=parent.origin.x+parent.size.width/2;
  154. // double cy=parent.origin.y+parent.size.height/2;
  155. CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
  156. if([vAlign.lowercaseString isEqualToString:@"middle"])
  157. {
  158. rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
  159. }
  160. return rect;
  161. }
  162. +(NSString*) legalFilename:(NSString*) str
  163. {
  164. NSString* ret = str;
  165. //....... todo
  166. // < > / \ | : " * ?
  167. NSCharacterSet *charSet = [NSCharacterSet characterSetWithCharactersInString:@"<>/\\|:\"*?"];
  168. NSArray *arr = [str componentsSeparatedByCharactersInSet:charSet];
  169. ret = [arr componentsJoinedByString:@""];
  170. return ret;
  171. }
  172. @end