oc字符串操作
#import <Foundation/Foundation.h>
@interface LYBNsstring : NSObject
//1.
+(NSString *) reveseWordsOfString:(NSString *)string;
//2.
+(NSArray *)symmetryArray:(NSArray *)arr;
//3.
+(NSInteger)countWord:(NSString *)string;
//4.
+(NSInteger)countOfString:(NSString *)string withSubString:(NSString *)substring;
//5.
+(BOOL)is1000PhoneMail:(NSString *)str;
@end
#import "LYBNsstring.h"
@implementation LYBNsstring
//1.
+(NSString *) reveseWordsOfString:(NSString *)string{
NSMutableArray * arr = [[NSMutableArray alloc]init];
NSArray * array = [string componentsSeparatedByString:@" "];
[arr addObjectsFromArray:array];
NSInteger len = arr.count;
for (int i = 0; i<len/2; i++) {
[arr exchangeObjectAtIndex:i withObjectAtIndex:len-i-1];
}
NSString * str = [arr componentsJoinedByString:@" "];
return str;
}
//2.
+(NSArray *)symmetryArray:(NSArray *)arr{
NSMutableArray * array1 = [[NSMutableArray alloc]init];
[array1 addObjectsFromArray:arr];
NSInteger len = array1.count;
long i ;
for (i=0; i<len/2; i++) {
[array1 exchangeObjectAtIndex:i withObjectAtIndex:len-i-1];
}
NSArray * array2 = [arr arrayByAddingObjectsFromArray:array1];
return array2;
}
//3.
+(NSInteger)countWord:(NSString *)string{
NSInteger len = string.length;
long i;
int k =0;
for (i=1; i<len; i++) {
unichar ch = [string characterAtIndex:i];
unichar ch1 = [string characterAtIndex:i-1];
if (!isalpha(ch)&&isalpha(ch1)) {
k++;
}
}
return k;
}
//4.
+(NSInteger)countOfString:(NSString *)string withSubString:(NSString *)substring{
NSInteger len = string.length;
NSMutableString * str = [[NSMutableString alloc]init];
[str appendString:string];
long i ;
int k=0;
for (i = 0; i<len;) {
NSRange rang = [str rangeOfString:substring];
if (rang.location!=NSNotFound) {
k++;
NSString * stt=[str substringFromIndex:rang.location+rang.length+1];
[str setString:stt];
}
else{
break;
}
}
return k;
}
//5.
+(BOOL)is1000PhoneMail:(NSString *)str{
NSInteger len = str.length;
NSString * str1 = @"@1000phone.com";
if ([str hasSuffix:str1]==NO) {
return NO;
}
NSString * str2 = [str substringToIndex:len-str1.length];
for (int i = 0; i<str2.length;i++)
{
unichar ch = [str2 characterAtIndex:i];
if (!isalpha(ch)&&!isalnum(ch)&&(ch!='_'))
{
return NO;
}
else{
return YES;
}
}
return YES;
}
@end
#import <Foundation/Foundation.h>
#import "LYBNsstring.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
//1.
NSLog(@"%@",[LYBNsstring reveseWordsOfString:@"Welcome to Beijing"] );
//2.
NSLog(@"%@",[LYBNsstring symmetryArray:@[@"one", @"Two", @"Three"]]);
//3.
NSLog(@"%ld",(long)[LYBNsstring countWord:@"Yes, she*is%my@love."]);
//4.
NSLog(@"%ld",[LYBNsstring countOfString:@"Wellcome to Beijing,come to here" withSubString:@"come"]);
//5.
NSLog(@"%d",[LYBNsstring is1000PhoneMail:@"$mail@1000phone.com"]);
}
return 0;
}