OC 数字对象和字符串对象

来源:互联网 发布:淘宝小号用什么注册 编辑:程序博客网 时间:2024/05/21 22:47

一 知识点



二 经典分析



三 (1)数字对象题目针对练习


练习题目1

- (void) t_Number

{

    //C中的基本数据类型转化成数字对象

    

    //1)int

    int number = 100;

    NSNumber *intNumber = [NSNumbernumberWithInt:number];

    NSLog(@"%@ ", intNumber);

    

    //2)char

    char c_number = 'c';

    NSNumber *charNumber = [NSNumbernumberWithChar:c_number];

    NSLog(@"%@ ", charNumber);

    

    //3)float

    float f_number = 3.14;

    NSNumber *flouatNumber = [NSNumbernumberWithFloat:f_number];

    NSLog(@"%@ ", flouatNumber);

    

    //4)double

    double d_number = 10.01;

    NSNumber *doubleNumber = [NSNumbernumberWithDouble:d_number];

    NSLog(@"%@ ", doubleNumber);

    

    //5)bool

    BOOL b_number =NO;

    NSNumber * boolNumber = [NSNumbernumberWithBool:b_number];

    NSLog(@"%@ ", boolNumber);

    

}


练习题目2

- (void)t2_Number

{

    //数字对象转化成 C中的基本数据类型

    

    //1)int

    NSNumber *intNumber;

    int i_number = [intNumber intValue];

    NSLog(@" %d ", i_number);

    

    //2)char

    NSNumber *charNumber;

    char c_number = [charNumbercharValue];

    NSLog(@"%c ", c_number);

    

    //3)float

    NSNumber *floatNumber;

    float f_number = [floatNumberfloatValue];

    NSLog(@"%f ", f_number);

    

    //4)double

    NSNumber *doubleNumber;

    double d_number = [doubleNumberdoubleValue];

    NSLog(@"%f ", d_number);

    

    //5)bool

    NSNumber *boolNumber;

    BOOL b_number = [boolNumberboolValue];

    NSLog(@"%d", b_number);

    

    //2. 求三个数字对象的最小值(使用对象的compare方法比较)

    [selfcompare_Number];

}


练习题目3

-(void)compare_Number

{

    NSNumber *aNumber = [NSNumbernumberWithInt:10];

    NSNumber *bNumber = [NSNumbernumberWithInt:50];

    NSNumber *cNumber = [NSNumbernumberWithInt:20];

    

    int result1 = [bNumbercompare:aNumber];//b a的差

    int result2 = [cNumbercompare:aNumber];//c a的差

    int result3 = [bNumbercompare:cNumber];//b c的差

    

    //题目2 //求三个数字对象的最小值(使用对象的compare方法比较)

    if (  result1 < 0 && result2 < 0)

    {

        NSLog(@"aNumber is min ");

    }

    if( result1 > 0 && result3 >0 )

    {

        NSLog(@"bNumber is min");

    }

    if ( result2 < 0 && result2 > 0)

    {

        NSLog(@"cNumber is min");

    }


    

#if 0 

    //参考简化

    int a, b, c;

    

    if ( a < b && a < c)

    {

        NSLog(@"a is min");

    }

    if (b < a && b < c)

    {

        NSLog(@"b is min");

    }

    if (c < a && c < b)

    {

        NSLog(@"c is min");

    }

    

#endif

    

    //题目3求三个数字对象的最大值(使用对象的compare方法比较)

    // a > b && a > c

    // b > a && b > c

    // c > a && c > b

    

    if ( result1 < 0 && result2 < 0)

    {

         NSLog(@"a is max");

    }

    if ( result1 > 0 && result3 > 0)

    {

         NSLog(@"b is max");

    }

    if ( result2 > 0 && result3 < 0)

    {

         NSLog(@"c is max");

    }


    //4. 求三个数字对象的最大值与最小值(使用对象的compare方法比较)

    // a > b && a > c , b > c

    // b > a && b > c , a > c

    // c > a && c > b,  a > b

    

    if ( result1 < 0 && result2 < 0)

    {

        if ( result3 > 0)

        {

            NSLog(@"aNumber is max");//a = max

            NSLog(@"cNumber is min");//c = min

        }else

        {

            NSLog(@"aNumber is max");//a = max

            NSLog(@"bNumber is min");//b = min

        }

    }

    if (result1 > 0 && result3 > 0 )

    {

        if (result2 < 0)

        {

            NSLog(@"bNumber is max");//b = max

            NSLog(@"aNumber is min");//a = min

        }

        else

        {

             NSLog(@"bNumber is max");//b = max

             NSLog(@"cNumber is min");//c = min

        }

    }

    if ( result2 > 0 && result3 > 0)

    {

        if (result1 < 0)//a > b

        {

            NSLog(@"cNumber is max");//c = max

            NSLog(@"bNumber is min");//b = min

        }

        else

        {

            NSLog(@"cNumber is max");//c = max

            NSLog(@"aNumber is min");//a = min

        }

    }

    

}





三 (2)字符串对象题目针对练习


练习题目1

- (void)t_String

{

    //NSString字符串 1.使用以下初始化方法: string  initWithString: initWithFormat: stringWithFormat:

    //分别创建一个字符串对象。

    

    //1)string

    NSString * string0 =@"aa";

    NSLog(@"%@ ", string0);

    

    //2)initWithString

    NSString *string1 = [[NSStringalloc]initWithString:@"bb"];

    NSLog(@"%@ ", string1);

    

    //2)initWithFormat

    int num = 10;

    NSString *string2 = [[NSStringalloc]initWithFormat:@"%d",num];

    NSLog(@"%@", string2);

    

    //3)stringWithFormat

    int num1 = 20;

    NSString *string3 = [NSStringstringWithFormat:@"%d", num1];

    NSLog(@"%@", string3);

    


}


练习题目2

- (void) compare_string

{

    //2. 定义三个字符串对象@"hello,string”, @“test”, @“baby”,找出最大字符串与最小字符串并打印出来(使用字符串compare方法)


    NSLog(@"进入字符串比较!!!");

    

    

    //把字符串对象转化成字符串

    NSString *aStr = [[NSStringalloc]initWithString:@"hello,string"];

    NSString *bStr = [[NSStringalloc]initWithString:@"test"];

    NSString *cStr = [[NSStringalloc]initWithString:@"baby"];

    

    int result1 = [aStrcompare: bStr];//a - b 结果是整型值

    int result2 = [aStrcompare: cStr];//a - c

    int result3 = [bStrcompare: cStr];//b - c

    

    // a > b && a > c , b > c

    // b > a && b > c , a > c

    // c > a && c > b,  a > b

    

    if ( result1 > 0 & result1 > 0)//  aStr is Max

    {

        if ( result3 > 0)

        {

            NSLog(@"aStr is max");//aStr = max

            NSLog(@"cStr is min");//cStr = min

        }else

        {

            NSLog(@"aStr is max");//aStr = max

            NSLog(@"bStr is min");//bStr = min

        }

    }

    if ( result1 < 0 & result3 > 0)// bStr is Max

    {

        if ( result2 > 0)

        {

            NSLog(@"bStr is max");//bStr = max

            NSLog(@"cStr is min");//cStr = min

        }

        else

        {

            NSLog(@"bStr is max");//bStr = max

            NSLog(@"aStr is min");//aStr = min

        }

    }

    if ( result2 < 0 & result3 < 0)// cStr is Max

    {

        if ( result1 > 0)

        {

            NSLog(@"cStr is max");//cStr = max

            NSLog(@"bStr is min");//bStr = min

        }

        else

        {

            NSLog(@"cStr is max");//cStr = max

            NSLog(@"aStr is min");//aStr = min

        }

    }


}



练习题目3


- (void) s_string

{

    //3. 编写代码举例说明以下字符串方法的使用:

    //length

    //uppercaseString

    //lowercaseString

    //capitalizedString

    //boolValue

    //floatValue

    //doubleValue

    //intValue

    

    NSLog(@"字符串关键字使用");

    

    //1)length 长度

    NSString *str1 = [[NSStringalloc]initWithFormat:@"aaaaa"];

    int l_strng = (int)[str1length];

    NSLog(@"%d ", l_strng);

    

    //2)uppercaseString || lowercaseString || capitalizedString   intValue字符串大小写转换

    

    NSString *str2 =@"bbbbbb";

    NSLog(@" %@ ", [str2uppercaseString]);

    NSLog(@" %@ ", [str2lowercaseString]);

    NSLog(@" %@ ", [str2capitalizedString]);

    

    //3)boolValue || floatValue || doubleValue || intValue || intValue

    

    NSString *str3_b =@" 0 ";

    NSLog(@"%d" , [str3_bboolValue]);//转换成BOOL类型

    

    NSString *str3_f =@" 3.14 ";

    NSLog(@" %f ", [str3_ffloatValue]);//转换成浮点型

    NSString *str3_d =@" 5.12 ";

    NSLog(@" %f ", [str3_ddoubleValue]);//转换成双精度

    

    NSString *str3_i =@" 10 ";

    NSLog(@" %d ", [str3_iintValue]);//转化成整形

    


}



练习题目4


- (void)transform_string

{

    //4.把字符串@"welcom to yi da hu lian”首字母转换成大写打印输出,全部大写并打印输出,全部小写并打印输出。

    NSLog(@"题目4");

    

    NSString *str4 = @"welcom to yi da hu lian";

    NSLog(@" %@ ", [str4capitalizedString]);//首字母转换成大写打印输出

    

    NSLog(@" %@ ", [str4uppercaseString]);//全部大写并打印输出

    

    NSLog(@" %@ ", [str4lowercaseString]);//全部小写并打印输出

    NSLog(@" %@ ", str4);


}


练习题目5

- (void)separate_string

{

    //5.把字符串@"welcom to china,my friends”里的单词全部分离并打印输出.

    NSLog(@"题目5");

    

    NSString *str5 = @"welcom to china,my friends";//对象字符串

    

    NSArray *array = [str5componentsSeparatedByString:@","];//字符串对象转化成数组

    

    int cont = (int) [arraycount];

    NSLog(@" 5cont =%d ", cont);

    for (int i = 0;  i < cont ; i++)

    {

        NSString *str = array[i];

        NSLog(@" %@ ", str );

    }

    

}


练习题目6

- (void) separate_string_C

{

    //6.编写代码把字符串@"welcom to xcode,hello swift”里的单词分离出,以每个单词首字母大写形式输出。

    

    NSLog(@"题目6");

    

    NSString *str6 =@"welcom to xcode,hello swift";

    

    NSArray *array = [str6componentsSeparatedByString:@","];//先分离字符串转化成数组

    

    int N = (int)[arraycount];

    for (int i = 0; i < N; i++)

    {

        NSString *str = array[i];

        NSLog(@" %@ ", [strcapitalizedString]);

    }

    

}


练习题目7

- (void) separate_string_cc

{

    //7.把题目6@"welcom to xcode,hello swift”字符串改成新字符串@"Welcom To Xcode,HELLO SWIFT”并打印输出新字符串。

    

    NSLog(@"题目7");

    

    NSString *str6 =@"welcom to xcode,hello swift";

    NSArray *array = [str6componentsSeparatedByString:@","];//先分离字符串转化成数组

   

    for (int i = 0; i < array.count ; i++)

    {

        if ( i < 1)

        {

            NSString *str = array[i];

            NSLog(@" %@ ", [strcapitalizedString ]);//首字母大写

        }

        else

        {

            NSString *str = array[i];

            NSLog(@" %@ \n", [struppercaseString]);//全部大写

        }

    }

}


练习题目8

- (void)str_manipulation

{

    //字符串前缀后缀检测/字符串UTF8String编码/单个字符获取

    //8. 有一字符串@“http://www.ios.com”,判断字符串是否包含前缀http,是否包含后缀.cn


    NSLog(@"题目8");

    NSString * str8 =@"http://www.ios.com";

    

    if ( [str8hasPrefix:@"http"] )

    {

        NSLog(@"%@包含前缀 %@ ", str8,@"http");

    }

    else

    {

        NSLog(@"%@没有包含前缀 %@ ", str8,@"http");

    }

    

    if ( [str8hasPrefix:@"cn"])

    {

         NSLog(@"%@包含 %@后缀 ", str8,@"cn");

    }

    else

    {

        NSLog(@"%@没有包含后缀 %@ ", str8,@"cn");

    }

    

}


练习题目9

- (void)str_manipulation9

{


    //9. 打印输出@"http://www.ios.com"中包含字符‘i‘,’o’,’s’,’n’对应的所引,找不到则不打印。

   

    NSLog(@"题目9");

    

    //NSString *str9 = @"http://www.ios.com";

    

    NSString *str9 = [[NSStringalloc]initWithFormat:@"http://www.ios.com"];

    int strlen = (int)[str9length];//获取字符串长度

    NSLog(@"%d ", strlen);

    

    for (int i = 0; i < strlen; i++)

    {

        char substr = [str9characterAtIndex:i];//获取单个字符

        //NSLog(@" %c ", substr);

        if ( substr == 'i')

        {

            NSLog(@"i的索引%d", i);//i的索引

        }

        if ( substr == 'o')

        {

            NSLog(@"o的索引%d", i);//o的索引

        }

        if ( substr == 's')

        {

            NSLog(@"s的索引%d", i);//s的索引

        }

        if ( substr == 'n')

        {

            NSLog(@"n的索引%d", i);//n的索引

        }

    }

    [str9 release];//释放

    

}


练习题目10

- (void)str_manipulation10

{

    //10.写一个字符串加密算法,对字符串对象@"helloboy"进行加密,加密后的密文是@"IFMMPCPZ",输出该密文。(根据字符的ASCII码可知,小写字母与大写字母数值差为32。即 ‘h'-32就是字符‘H’)

    

    NSLog(@"题目10");

    

    NSString *str10 = [[NSStringalloc]initWithFormat:@"helloboy"];

    //NSString *newstr = [str10 uppercaseString];//字符串转化成全部大写

    

    int strlen = (int)[str10length];//字符串长度

    for (int i = 0; i < strlen; i++)

    {

        char substr = [str10characterAtIndex:i] - 31;//通过ANSII

        NSLog(@"%c", substr);

    }

    [str10 release];//释放

    

}


练习题目11

- (void)str_cut

{


    //11. 把字符串@"thank you very much, my friends!"截取成@"very much, my”,截取成功后把字符串@"dear students!"拼接其后并打印输出。

    NSLog(@"题目11");

    

    NSString * str11 =@"hank you very much, my friends!";

    NSString * str =@"dear students!";

    

    NSRange str_rang;

    str_rang.location = 8;

    str_rang.length = 14;

    

    NSString *newstr = [str11substringWithRange:str_rang];//1)实现字符串截取

    NSLog(@" %@ ", newstr);

    

    NSString *newstr2 = [newstrstringByAppendingString:str];//2)实现字符串拼接

    

    NSLog(@" %@ ", newstr2);//3)打印输出

    

}


练习题目12

- (void)str_cut2

{

    //12. 有一字符串@“http://www.ioscom”,获取子串@“www.ios.com”并打印输出。

    NSLog(@"题目12");

    

    

    NSString *str12 =@"http://www.ios.com";

    

    NSString *substr = [str12substringFromIndex:7];

    NSLog(@" %@ ", substr);

    

}


练习题目13


- (void)str_cut3

{

    //13.把一段英文@“Today is FridayThis is the last weekdaytomorrow is saturdayafter saturday is sunday”,编写代码把包含@"day"的单词全部改成大写(比如:"Today"包含"day",要把"Today"改成"TODAY"),打印输出改写后的这段新英文。

    

    NSLog(@"题目13");

    

    NSString *str13 =@"Today is Friday! This is the last weekday! tomorrow is saturday! after saturday is sunday";

    

    NSArray *array = [str13componentsSeparatedByString:@" "];//1)分离成每个数组

    NSString * newstr = [[NSStringalloc]init];//分配空间

    NSString *str_all = [[NSStringalloc]init];//分配空间

    

    for (int i = 0;  i < array.count; i++)

    {

        newstr = array[i];//2)遍历数组赋值给字符串对象

        

        NSRange range = [newstrrangeOfString:@"day"];//3)查询每个包含day

        if ( range.location !=NSNotFound)

        {

            newstr = [newstr uppercaseString];//4)转化成大写

        }

        

       str_all = [str_all stringByAppendingFormat:@"%@", newstr];//5)拼接字符串

    }

    NSLog(@"%@",str_all);

    

    [newstr release];//释放空间

    [str_all release];//释放空间

}


练习题目14


- (void)find_string

{

    //14. 有一字符串@"welcom to China, My Friends!”,查找字符串@“China”,若存在打印它所在的位置所引,不存在打印 -1

    

    NSLog(@"题目14");

    

    NSString *str14 =@"welcom to China, My Friends!";

    //NSString *substr = @"China";

    

    NSRange range = [str14rangeOfString:@"China"];

    if (range.location !=NSNotFound)

    {

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

    }

    else

    {

        NSLog(@"不存在打印-1");

    }


}


练习题目15

- (void)find_str2

{

    //15.查找字符串@"today, we will study another string, this string is import for us, now begin the string!"中是否含有@"string",输出它们的所有位置。

    

    NSLog(@"题目15");

    

    NSString *str15 =@"today, we will study another string, this string is import for us, now begin the string!";

    

    NSArray *array = [str15componentsSeparatedByString:@"string"];//分离

    

    int len = (int)[arraycount];//整个字符串对象分离后的的数组长度

    int lon0 = 0;int lon1 = 0;int lon2 =0;

    for (int i = 0; i < len; i++)

    {

        NSString *str = array[i];//每次分离的数组转化为每次新的字符串对象

        

        //NSLog(@"str=%@ i= %d@", str, i);//打印出每个分离出的字符串

        

        if ( i == 0 )

        {

            lon0  = (int)[strlength];//每次字符串的长度

            NSLog(@"lon0 = %d", lon0);

        }

        if ( i == 1)

        {

            lon1 = (int)[strlength] + lon0;

            NSLog(@"lon1 = %d", lon1);

        }

        if ( i == 2)

        {

            lon2 = (int)[strlength] + lon0;

            NSLog(@"lon2 = %d", lon2);

        }


    }

    

}


练习题目16

- (void)delete_str1

{

    //16. 删除题目15@"today, we will study another string, this string is import for us, now begin the string!"所有的@"string"字符串并打印删除后的新英文。

    

    NSLog(@"题目16");

    NSMutableString *str16 = [NSMutableStringstringWithString:@"today, we will study another string, this string is import for us, now begin the string!"];//定义可变字符串

    NSLog(@"删除string=%@", str16);

    int location = 0, length = 0;


    //第一个字符串

    NSRange range = [str16rangeOfString:@"string"];

    if ( range.location  !=NSNotFound)

    {

        location = (int)range.location;//查询到string的位置

        length = (int)range.length;//查询到string的长度

        NSLog(@"location=%dlength=%d", location, length);

        

        [str16 deleteCharactersInRange:NSMakeRange(location, length)];//根据查询获取的位置和长度删除string

    }

    //NSLog(@"删除第1string%@", str16);

    

    NSRange range1 = [str16rangeOfString:@"string"];

    if ( range1.location  !=NSNotFound)

    {

        location = (int)range1.location;//查询到string的位置

        length = (int)range1.length;//查询到string的长度

        NSLog(@"location=%dlength=%d", location, length);

        

        [str16 deleteCharactersInRange:NSMakeRange(location, length)];//根据查询获取的位置和长度删除string

    }

    //NSLog(@"删除第2string%@", str16);


    NSRange range2 = [str16rangeOfString:@"string"];

    if ( range2.location  !=NSNotFound)

    {

        location = (int)range2.location;//查询到string的位置

        length = (int)range2.length;//查询到string的长度

        NSLog(@"location=%dlength=%d", location, length);

        

        [str16 deleteCharactersInRange:NSMakeRange(location, length)];//根据查询获取的位置和长度删除string

    }

    //NSLog(@"删除第3string%@", str16);

    

    NSLog(@"删除string=%@", str16);

    

}


练习题目17

-(void)find_str4

{

    //17.在题目15@"today, we will study another string, this string is import for us, now begin the string!"字符串的逗号后面插入字符串@"msn"并打印输出新的英文。

    

    NSLog(@"题目17");


#if 0

    //通过可变字符串处理

    

    NSMutableString *str17 = [NSMutableString stringWithFormat:@"today, we will study another string, this string is import for us, now begin the string!"];

    NSMutableString *str_all = [NSMutableString string];

    

    NSArray *array = [str17 componentsSeparatedByString:@","];//分离字符串对象

    

    for (int i = 0;  i < array.count; i++)

    {

        [str_all appendFormat:@"%@,msn", array[i]];//逐个追加把@"msn"插入到每个','后面

    }

    NSLog(@"%@", str_all);

    

#else

    

    //通过不可变字符串来处理

    

    NSString *str17 =@"today, we will study another string, this string is import for us, now begin the string!";

    NSString *str_all = [[NSStringalloc]init];


    NSArray *array = [str17componentsSeparatedByString:@","];//分离字符串对象

    

    for (int i = 0; i < array.count-1; i++)

    {

        str_all = [str_all stringByAppendingFormat:@"%@,msn", array[i]];//添加分离的数组添加msn存放到str_all数组中

    }

    str_all=[str_all stringByAppendingString:array[array.count-1]];//追加最后面的1个分离出来的数组内容

    

    NSLog(@"%@", str_all);

    

    [str_all release];

#endif

    

}


练习题目18

- (void)chang_str

{

    //NSMutableString可变字符串

    //18.定义一个可变字符串@"today, we will study another string, this string is import for us, now begin the string!",把含有@"string"字符串替换成@"STRING",打印输出替换后的新英文。

    

    NSLog(@"题目18");

    

    NSMutableString *str18 = [NSMutableStringstringWithString:@"today, we will study another string, this string is import for us, now begin the string!"];

    int location = 0;

    int length = 0;


    NSRange range = [str18rangeOfString:@"string"];

    if ( range.location !=NSNotFound)

    {

        location = (int)range.location;

        length = (int)range.length;

        

        [str18 replaceCharactersInRange:NSMakeRange(location, length)withString:@"STRING"];

    }

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

    

    NSRange range1 = [str18rangeOfString:@"string"];

    if ( range1.location !=NSNotFound)

    {

        location = (int)range1.location;

        length = (int)range1.length;

        

        [str18 replaceCharactersInRange:NSMakeRange(location, length)withString:@"STRING"];

    }

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

    

    NSRange range2 = [str18rangeOfString:@"string"];

    if ( range2.location !=NSNotFound)

    {

        location = (int)range2.location;

        length = (int)range2.length;

        

        [str18 replaceCharactersInRange:NSMakeRange(location, length)withString:@"STRING"];

    }

    NSLog(@"%@ ", str18);//处理后打印

    

}


练习题目19


-(void)delete_str

{


    //19.删除题目18@"today, we will study another STRING, this STRING is import for us, now begin the STRING!"所有@"STRING"字符串,打印输出删除后的新英文。

    

    NSLog(@"题目19");

    

    NSMutableString *str18 = [NSMutableStringstringWithString:@"today, we will study another STRING, this STRING is import for us, now begin the STRING!"];

    

    NSRange range = [str18rangeOfString:@"STRING"];

    if ( range.location !=NSNotFound)

    {

        [str18 deleteCharactersInRange:NSMakeRange(range.location, range.length)];

    }

    NSLog(@"%@", str18);//删除字符串STRING

    

    NSRange range1 = [str18rangeOfString:@"STRING"];

    if ( range1.location !=NSNotFound)

    {

        [str18 deleteCharactersInRange:NSMakeRange(range1.location, range1.length)];

    }

    NSLog(@"%@", str18);//删除字符串STRING

    

    NSRange range2 = [str18rangeOfString:@"STRING"];

    if ( range2.location !=NSNotFound)

    {

        [str18 deleteCharactersInRange:NSMakeRange(range2.location, range2.length)];

    }

    NSLog(@"%@", str18);//删除字符串STRING

}


练习题目20

- (void)insert_str

{

    //20. 在题目18字符串的逗号后面插入字符串@"msn",打印输出新的英文。

    

    NSLog(@"题目20");


    NSMutableString *str20 = [NSMutableStringstringWithString:@"today, we will study another string, this string is import for us, now begin the string!"];

    NSMutableString *newstr = [NSMutableStringstring];//定义可变字符串

    

    NSArray *array = [str20componentsSeparatedByString:@","];//分离

    

    for (int i = 0; i < array.count; i++)

    {

        NSString  *new_str = array[i];//每个分离出来的字符串存放到新定义的字符串中

        

        [newstr appendFormat:@"%@", new_str];//逐个追加

        

        if ( i != array.count -1)//判断如果是数组最后的元素不插入msn

        

        [newstr insertString:@"msn"atIndex:newstr.length ];//根据每个分离获取新的字符串长度插入msn

    }

    NSLog(@"20=%@", newstr);

}


练习题目21

- (void)str_encrypt

{

    //21.优化之前的字符串加密算法,对字符串对象@"helloboy"进行加密,利用可变字符串接收加密后的密文@"IFMMPCPZ",打印输出该密文。(根据字符的ASCII码可知,小写字母与大写字母数值差为32。即 'h'-32就是字符'H')

    

    NSLog(@"题目21");


    NSMutableString *str = [NSMutableStringstring];//可变字符串

    NSString *str21 = [[NSStringalloc]initWithFormat:@"helloboy"];//字符串对象

    

    for (int i = 0; i < str21.length; i++)

    {

        char c = [str21characterAtIndex:i] - 31;//通过ANSII

        [str appendFormat:@"%c", c];//逐个添加字符

    }

    NSLog(@"%@", str);

    

    

}


0 0
原创粉丝点击