OC6_字符串练习

//

//  QFString.h

//  OC6_字符串练习

//

//  Created by zhangxueming on 15/6/10.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>



@interface QFString : NSObject



//比如输入 @"abcdef"输出@"fedcba"

+(NSString *)reveserString:(NSString *)srcStr;

//压缩字符串

//比如输入@"aaabbbcceaa" 输出@"3a3b2c1e2a"

+(NSString *)firstContinueString:(NSString *)string;



+(NSString *)compressString:(NSString *)srcStr;



//对字符串按字母顺序进行排序

//比如输入@"aczabczab"  输出@"aaabbcczz"

+(NSString*)sortString:(NSString*)srcStr;



//将原字符串进行小写转大写,大写转小写

//比如输入 @"aBCdEfGA" 输出 @"AbcDeFga"

+(NSString *)reveserCharacters:(NSString*)srcStr;



// 替换字符串srcStr中从startKey到endKey之间的字符串//返回结果

//比如输入srcStr=@"abcdaabbccdef" startKey=@"da" endKey=@"cde" 输出 @"abbc"

+(NSString *)replaceString:(NSString *)srcStr betweenStartKey:(NSString* )startKey  andEndKey:(NSString*)endKey;



// 返回字符串srcStr中出现子串findStr的次数

//helloworldhelloworldhello  hello

+(NSInteger)subStringCount:(NSString*)srcStr findStr:(NSString*)findStr;









+(void)testString;



@end
//

//  QFString.m

//  OC6_字符串练习

//

//  Created by zhangxueming on 15/6/10.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import "QFString.h"



@implementation QFString



//@"hello world"

+ (NSString *)reveserString:(NSString *)srcStr

{

    NSUInteger len = [srcStr length];

    NSString *dstString = @"";

    for (NSInteger i=len-1; i>=0; i--) {

        unichar ch = [srcStr characterAtIndex:i];

        dstString = [dstString stringByAppendingFormat:@"%C", ch];

    }

    return dstString;

}

//@"aaabbcccccdddd"

+ (NSString *)firstContinueString:(NSString *)string

{

    NSInteger len = [string length];

    NSInteger i;

    for (i=1; i<len; i++) {

        unichar ch1= [string characterAtIndex:i-1];

        unichar ch2= [string characterAtIndex:i];

        if (ch1 != ch2) {

            break;

        }

    }

    return [string substringToIndex:i];

}

//@"aaabbcccccdddd" --> 3a2b5c4d  @""

+ (NSString *)compressString:(NSString *)srcStr

{

    NSString *dstString = @"";

    NSString *tempStr = srcStr;

    while ([tempStr length]) {

        NSString *subString = [self  firstContinueString:tempStr];

        dstString = [dstString stringByAppendingFormat:@"%ld%C",[subString length], [subString characterAtIndex:0]];

        tempStr = [tempStr substringFromIndex:[subString length]];

    }

    return dstString;

}



+ (NSString *)sortString:(NSString *)srcStr

{

    NSInteger len = [srcStr length];

    for (NSInteger i=0; i<len-1; i++) {

        for (NSInteger j=0; j<len-i-1; j++) {

            unichar ch1 = [srcStr characterAtIndex:j];

            unichar ch2 = [srcStr characterAtIndex:j+1];

            if (ch1 > ch2) {

                srcStr = [srcStr stringByReplacingCharactersInRange:NSMakeRange(j, 1) withString:[NSString stringWithFormat:@"%C", ch2]];

                srcStr = [srcStr stringByReplacingCharactersInRange:NSMakeRange(j+1, 1) withString:[NSString stringWithFormat:@"%C", ch1]];

            }

        }

    }

    return srcStr;

}



//将原字符串进行小写转大写,大写转小写

//比如输入 @"aBCdEfGA" 输出 @"AbcDeFga"

//abcdefga

+(NSString *)reveserCharacters:(NSString*)srcStr

{

    NSString *dstString = [srcStr lowercaseString];

    for (NSInteger i=0; i<[dstString length]; i++) {

        unichar ch = [dstString characterAtIndex:i];

        if ( ch == [srcStr characterAtIndex:i]) {

            dstString = [dstString stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:[NSString stringWithFormat:@"%C", (unichar)(ch-32)]];

        }

    }

    return dstString;

}



+(NSString *)replaceString:(NSString *)srcStr betweenStartKey:(NSString* )startKey  andEndKey:(NSString*)endKey

{

    

    return nil;

}



// 返回字符串srcStr中出现子串findStr的次数

//worldhelloworldhello  hello

+(NSInteger)subStringCount:(NSString*)srcStr findStr:(NSString*)findStr

{

    NSInteger cnt = 0;

    NSString *tempString = srcStr;

    NSRange range ;

    while ((range = [tempString rangeOfString:findStr],range.location != NSNotFound)) {

        cnt++;

        tempString = [tempString substringFromIndex:range.location+range.length];

    }

    return cnt;

}





+ (void)testString

{

    NSLog(@"%@", [self reveserString:@"hello world"]);

    NSLog(@"%@", [self firstContinueString:@"aaabbcccccdddd"]);

    NSLog(@"%@", [self compressString:@"aaabbcccccdddd"]);

    NSLog(@"%@", [self sortString:@"oiuytredfghjk"]);

    NSLog(@"%@", [self reveserCharacters:@"ASDDsdfsfAFAFA"]);

    NSLog(@"cnt = %li", [self subStringCount:@"helloworldhelloworldhello" findStr:@"hello"]);

}

@end
//

//  main.m

//  OC6_字符串练习

//

//  Created by zhangxueming on 15/6/10.

//  Copyright (c) 2015年 zhangxueming. All rights reserved.

//



#import <Foundation/Foundation.h>

#import "QFString.h"

//4. 现有字符串@"When I was young, I loved a girl on neighbor class."要求不通过点数,从was提取到girl。



//7. 现有字符串@"When I was young, I loved a girl on neighbor class. "从young到girl,改成@"teacher, I rather teaching the students



int main(int argc, const char * argv[]) {

    @autoreleasepool {

#if 0

        NSString *str1 = @"When I was young, I loved a girl on neighbor class.";

        NSRange range1 = [str1 rangeOfString:@"was"];

        NSRange range2 = [str1 rangeOfString:@"girl"];

        if (range1.location != NSNotFound && range2.location != NSNotFound && range2.location > range1.location) {

            NSString *subString = [str1 substringWithRange:NSMakeRange(range1.location, range2.location-range1.location+range2.length)];

            NSLog(@"subString = %@", subString);

        }

        

        NSRange range3 = [str1 rangeOfString:@"young"];

        if (range3.location != NSNotFound && range2.location != NSNotFound && range2.location > range3.location) {

            NSString *str2 = [str1 stringByReplacingCharactersInRange:NSMakeRange(range3.location, range2.location-range3.location+range2.length) withString:@"teacher, I rather teaching the students"];

            NSLog(@"str2= %@", str2);

        }

        

#endif

        [QFString testString];

    }

    return 0;

}

 

你可能感兴趣的:(字符串)