OC一些字符串的练习

//

//  main.m

//  字符串处理

//

//  Created by qianfeng on 15-9-15.

//  Copyright (c) 2015 千锋. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "MyNSString.h"

////1. 现在有两个数字分别是123,1.23 把这两个数字转为字符串

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

//{

//    @autoreleasepool

//    {

//        int i=123;

//        float f=1.23;

//        NSString *intString = [NSString stringWithFormat:@"%d",i];

//        NSLog(@"%@",intString);

//        NSString *floatString = [NSString stringWithFormat:@"%f",f];

//        NSLog(@"%@",floatString);

//    }

//    return 0;

//}


////2. 现在有一个C的字符串”I love you very much!”,请把它转为一个OC的字符串

//int main()

//{

//    @autoreleasepool {

//        char *s = "I love you very much!";

//        NSString *str = [NSString stringWithUTF8String:s];

//        NSLog(@"%@",str);

//    }

//}


///*

// 3. 有三个字符串,分别是

//str1 = @"My name is" str2 = @"Lilei" str3 = @"I am " int age = 20;

//拼接在⼀一起,生成⼀一个新字符串,str4.使语句通顺

//*/

//int main()

//{

//    @autoreleasepool {

//        NSString *str1 = @"My name is";

//        NSString *str2 = @" Lilei";

//        NSString *str3 = @" I am ";

//        int age = 20;

//        NSString *str = [NSString stringWithFormat:@"%d",age];

//        NSMutableString *str4 = [[NSMutableString alloc]init];

//        [str4 appendString:str1];

//        [str4 appendString:str2];

//        [str4 appendString:str3];

//        [str4 appendString:str];

//        NSLog(@"%@",str4);

//    }

//}



////4. 输入两个字符串,然后不区分大小写进行比较,输出比较结果; 要求实现一个字符串比较函数,不区分大小写进行比较,如果第一个字符串 大输出1,相等输出0,小于输出-1

//int main()

//{

//    @autoreleasepool {

//        NSString *str1 = @"abc";

//        NSString *str2 = @"Abc";

//        NSComparisonResult ret = [str1 caseInsensitiveCompare:str2];

//        if(ret == NSOrderedSame)

//        {

//            NSLog(@"0");

//        }

//        else if(ret == NSOrderedDescending)

//        {

//            NSLog(@"1");

//        }

//        else

//        {

//            NSLog(@"-1");

//        }

//    }

//}


////5.Welcome to Qianfeng. 查找feng在字符串中的范围;

//int main()

//{

//    @autoreleasepool {

//        NSString *src = @"Welcome to Qianfeng.";

//        NSString *sreach = @"feng";

//        NSRange range = [src rangeOfString:sreach];

//        NSLog(@"range.loctaion=%ld,range.length=%ld",range.location,range.length);

//    }

//}


////6.Welcome to Bejing.

////提取第3个到第6个字符.

//int main()

//{

//    @autoreleasepool {

//        NSString *src = @"Welcome to Bejing.";

//        NSString *ret = [src substringWithRange:NSMakeRange(3, 3)];

//        NSLog(@"%@",ret);

//    }

//}


////7.When I was young, I'd listen to the radio, waiting for my favorite songs. was提取到radio;不许数。

//int main()

//{

//    NSString *src = @"When I was young, I'd listen to the radio, waiting for my favorite songs.";

//    NSRange startRange = [src rangeOfString:@"was"];

//    NSRange endRange = [src rangeOfString:@"radio"];

//    NSString *ret = [src substringWithRange:NSMakeRange(startRange.location, endRange.length+endRange.location-startRange.location)];

//    NSLog(@"%@",ret);

//}


////8.I love iOS.删除love这个词。

//int main()

//{

//    @autoreleasepool {

//        char *s = "I love iOS.";

//        NSMutableString *src = [NSMutableString stringWithUTF8String:s];

//        NSRange range = [src rangeOfString:@"love"];

//        [src deleteCharactersInRange:range];

//        NSLog(@"%@",src);

//    }

//}


////10.Hello world Hello 后插入 Holly

//int main()

//{

//    @autoreleasepool {

//        char *s = "Hello world";

//        NSMutableString *src = [NSMutableString stringWithUTF8String:s];

//        NSRange range = [src rangeOfString:@"Hello"];

//        [src insertString:@"Holly" atIndex:range.length+range.location];

//        NSLog(@"%@",src);

//    }

//}


////11.When I was young, I loved a girl in neighbor class.younggirl,改成 (replace) teacher, I rather teaching the students

//int main()

//{

//    @autoreleasepool {

//        NSMutableString *src = [NSMutableString stringWithString:@"When I was young, I loved a girl in neighbor class."];

//        NSRange startRange = [src rangeOfString:@"young"];

//        NSRange endRange = [src rangeOfString:@"girl"];

//        [src replaceCharactersInRange:NSMakeRange(startRange.location, endRange.location+endRange.length-startRange.location) withString:@"teacher, I rather teaching the students"];

//        NSLog(@"%@",src);

//    }

//}


////14. 从终端输入一个文件的路径,要求把文件的内容显示到屏幕上

//int main()

//{

//    @autoreleasepool {

//        NSString *path = @"/Users/qianfeng/Desktop/Objective-c/OC练习/字符串处理/字符串处理/File";

//        NSString *ret = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

//        NSLog(@"%@",ret);

//    }

//}


///*

// 15.字符串处理

//将传入的字符串逆序

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

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

// */

//int main()

//{

//    @autoreleasepool {

//        NSString *str = @"abcdef将传入的字符串逆序";

//        NSString *ret = [MyNSString reveserString:str];

//        NSLog(@"%@",ret);

//    }

//}


///*

// //压缩字符串

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

// +(NSString*)compString:(NSString*)srcStr;

// */

//int main()

//{

//    @autoreleasepool {

//        NSString *str = @"aaabbbcceaa";

//        NSString *ret = [MyNSString compString:str];

//        NSLog(@"%@",ret);

//    }

//}


///*

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

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

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

// */

//int main()

//{

//    @autoreleasepool {

//        NSString *str = @"aczabczab";

//        NSString *ret = [MyNSString sortString:str];

//        NSLog(@"%@",ret);

//    }

//}


///*

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

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

// +(NSString *)reveserChacter:(NSString*)srcStr;

// */

//int main()

//{

//    @autoreleasepool {

//        NSString *str = @"aBCdEfGA";

//        NSString *ret = [MyNSString reveserChacter:str];

//        NSLog(@"%@",ret);

//    }

//}


///*

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

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

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

// */

//int main()

//{

//    @autoreleasepool {

//        NSString *srcStr = @"abcdaabbccdef";

//        NSString *startKey = @"da";

//        NSString *endKey = @"cde";

//        NSString *ret = [MyNSString replaceString:srcStr betweenStartKey:startKey andEndKey:endKey];

//        NSLog(@"%@",ret);

//    }

//}



#import <Foundation/Foundation.h>


@interface MyNSString : NSObject


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


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


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


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


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


@end


#import "MyNSString.h"


@implementation MyNSString


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

{

    NSString *ret = @"";

    for(NSInteger i=[srcStr length]-1;i>=0;i--)

    {

        unichar ch = [srcStr characterAtIndex:i];

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

    }

    return ret;

}


+(NSString*)compString:(NSString*)srcStr

{

    NSString *ret = @"";

    NSInteger count=0;

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

    {

        unichar ch = [srcStr characterAtIndex:i];

        if((i == [srcStr length]-1) || (ch != [srcStr characterAtIndex:i+1]))

        {

            count++;

            ret = [ret stringByAppendingFormat:@"%ld%C",count,ch];

            count = 0;

        }

        else

        {

            count++;

        }

    }

    return  ret;

}


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

{

    NSString *ret =srcStr;

    for(NSInteger i=0;i<[srcStr length]-1;i++)

    {

        for(NSInteger j=0;j<[srcStr length]-1-i;j++)

        {

            unichar jChar = [ret characterAtIndex:j];

            unichar jjChar = [ret characterAtIndex:j+1];

            NSString *jStr = [NSString stringWithFormat:@"%C",jChar];

            NSString *jjStr = [NSString stringWithFormat:@"%C",jjChar];

            NSComparisonResult com = [jStr compare:jjStr];

            if(com == NSOrderedDescending)

            {

                ret = [ret stringByReplacingCharactersInRange:NSMakeRange(j,1) withString:jjStr];

                ret = [ret stringByReplacingCharactersInRange:NSMakeRange(j+1,1) withString:jStr];

            }

        }

    }

    return  ret;

}


+(NSString *)reveserChacter:(NSString*)srcStr

{

    NSString *ret = srcStr;

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

    {

        unichar ch = [srcStr characterAtIndex:i];

        NSString *s = [NSString stringWithFormat:@"%C",ch];

        if([[s lowercaseString] isEqualToString:s])

        {

            ret = [ret stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:[s uppercaseString]];

        }

        else

        {

            ret = [ret stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:[s lowercaseString]];

        }

    }

    return ret;

}


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

{

    NSString *ret = srcStr;

    NSRange startRange = [srcStr rangeOfString:startKey];

    NSRange endRange = [srcStr rangeOfString:endKey];

    ret = [ret substringWithRange:NSMakeRange(startRange.location+startRange.length, endRange.location-startRange.location-startRange.length)];

    return ret;

}

@end

相关文章
相关标签/搜索