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