| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- //
- // TextUtils.m
- // AntsContract
- //
- // Created by Ray on 12/20/16.
- // Copyright © 2016 United Software Applications, Inc. All rights reserved.
- //
- #import "TextUtils.h"
- @implementation TextUtils
- + (long)countOccurencesOfString:(NSString*)string find:(NSString*)searchString {
- long strCount = string.length - [[string stringByReplacingOccurrencesOfString:searchString withString:@""] length];
- return strCount / [searchString length];
- }
- + (NSString *)formatNumber:(NSString *)mobileNumber
- {
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
-
- NSLog(@"%@", mobileNumber);
-
- int length = (int)[mobileNumber length];
- if(length > 10)
- {
- mobileNumber = [mobileNumber substringFromIndex: length-10];
- NSLog(@"%@", mobileNumber);
-
- }
-
- return mobileNumber;
- }
- + (int)getLength:(NSString *)mobileNumber
- {
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"(" withString:@""];
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@")" withString:@""];
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@" " withString:@""];
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"-" withString:@""];
- mobileNumber = [mobileNumber stringByReplacingOccurrencesOfString:@"+" withString:@""];
-
- int length = (int)[mobileNumber length];
-
- return length;
- }
- +(NSString*) encodePhoneNumber:(NSString*) phone
- {
-
-
-
-
-
- NSLocale* locale = [NSLocale currentLocale];
- if( [[locale localeIdentifier] compare:@"en_US"]!=NSOrderedSame )
- return phone;
-
- if( [phone length]==0 )
- return phone;
-
- if( [phone rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet] ].location==NSNotFound )
- {
- const char* string = [phone UTF8String];
- int length = [phone lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
- if( [[NSString stringWithCString:string length:1] intValue]==1 )
- {
- if( length > 11 )
- return phone;
- NSMutableData* array = [[NSMutableData alloc] init] ;
- // 1 (234) 567-8901
- for( int i=0; i < length; i++ )
- {
- if( i==1 )
- {
- [array appendBytes:" (" length:2];
- }
- if( i == 4 )
- {
- [array appendBytes:") " length:2];
- }
- if( i==7 )
- {
- [array appendBytes:" " length:1];
- }
- [array appendBytes:string++ length:1];
- }
- return [NSString stringWithUTF8String:(const char*)[array bytes]];
- }
- else
- {
- // (012) 345-6789
- if( length > 10 )
- return phone;
- NSMutableData* array = [[NSMutableData alloc] init] ;
- int i=0;
- if( length <=7 )
- {
- for( i=0; i < length; i++ )
- {
- if( i==3 )
- {
- [array appendBytes:"-" length:1];
- }
- [array appendBytes:string++ length:1];
- }
- }
- else
- {
- for( i=0; i < length; i++ )
- {
- if( i==0 )
- [array appendBytes:"(" length:1];
- if( i==3 )
- [array appendBytes:") " length:2];
- if( i==6 )
- [array appendBytes:"-" length:1];
- [array appendBytes:string++ length:1];
- }
- }
- [array appendBytes:"\0" length:1];
- return [NSString stringWithUTF8String:(const char*)[array bytes]];
- }
- }
- return phone;
- }
- +(NSString*) RegularExpReplace:(NSString*) content from:(NSString*) pattern to:(NSString*) to_pattern
- {
- if(content==nil)
- return nil;
-
-
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
- NSString* sss = [regex stringByReplacingMatchesInString:content options:0 range:NSMakeRange(0, content.length) withTemplate:to_pattern];
-
- return sss;
- }
- +(NSArray*) expression_varable:(NSString*)content regex:(NSString*) pattern
- {
-
- if(content==nil)
- return nil;
-
-
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
-
- NSArray *matches = [regex matchesInString:content options:nil range:NSMakeRange(0, content.length)];
-
- if (matches) {
- for (NSTextCheckingResult *match in matches) {
- for (int i = 0; i < match.numberOfRanges; ++i) {
- DebugLog(@"-> %@", [content substringWithRange:[match rangeAtIndex:i]]);
- }
- }
- }
- return matches;
- }
- +(NSTextCheckingResult*) expression_findfistMatch:(NSString*)content regex:(NSString*) pattern
- {
- NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:nil error:nil];
-
- NSTextCheckingResult *match = [regex firstMatchInString:content options:nil range:NSMakeRange(0, content.length)];
- return match;
- // if (matches) {
- // for (NSTextCheckingResult *match in matches) {
- // for (int i = 0; i < match.numberOfRanges; ++i) {
- // DebugLog(@"-> %@", [content substringWithRange:[match rangeAtIndex:i]]);
- // }
- // }
- // }
- // return matches;
- }
- + (CGRect)rectVAlign:(CGRect )parent rect:(CGRect)rect vAlign:(NSString*)vAlign
- {
- // double cx=parent.origin.x+parent.size.width/2;
- // double cy=parent.origin.y+parent.size.height/2;
- CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
-
- if([vAlign.lowercaseString isEqualToString:@"middle"])
- {
- rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
- }
-
- return rect;
-
- }
- @end
|