iOS学习——第二天练习题

来源:互联网 发布:b站 小学生 知乎 编辑:程序博客网 时间:2024/05/29 17:46

1.构建一个字符串(This is a string!),并打印出来 stringWithFormat stringWithString

2.判断字符串长度,并打印出来 length

取出字符串第3个位置的字符,并打印 characterAtIndex

3.构建2个字符串,把连个字符串拼接起来,并打印 stringByAppendingString

4.定义一个长度为20的字符串,取出前10个字符,并打印 substringToIndex

去掉前10个字符,并打印 substringFromIndex

5.把字符串全部改成大写,并打印 uppercaseString

全部改为小写,并打印 lowercaseString

6.构建2个字符串,比较大小,并打印结果 isEqualToString/compare

7.构建一个数值型的字符串,转化成int、bool、float、double类型 intValue boolValue floatValue doubleValue

8.构建字符串"My story is the type of twists and turns or ups and downs of melodrama“将这句话的单词拆分,并保存到NSArray数组里面,打印该数组 componentsSeparatedByString

#import"AppDelegate.h"


@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

   // Override point for customization after application launch.

    NSString *str=@"This is a string!";

   NSString *str1=[NSStringstringWithFormat:@"%@",@"This is a string!"];

    NSString *str2=[NSString stringWithString:str];

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

    

   NSInteger len=str1.length;//[str1 length];

    NSLog(@"len:%d",len);

    char c;

    c=[str1characterAtIndex:3];

    NSLog(@"%c",c);

    

    NSString *str3=@"hello";

    NSString *str4=@"world";

    NSString *str5=[str3 stringByAppendingString:str4];

    NSLog(@"%@",str5);

    

   NSString *str6=@"0123456789abcdefghij";

    NSString *str7=[str6 substringToIndex:10];

    NSString *str8=[str6 substringFromIndex:10];

    NSLog(@"前十 %@后十 %@",str7,str8);

    

   NSString *str9=@"HelloWorld";

    NSString *str10=[str9 uppercaseString];

    NSString *str11=[str9 lowercaseString];

    NSLog(@"大写 %@小写 %@",str10,str11);

    

    if([str3 isEqualToString:str4])

    {

        NSLog(@"两个字符串相等");

    }

    else

    {

        NSLog(@"两个字符串不相等");

    }

    

    NSInteger i=[str3 compare:str4];

    if(i==0)

    {

        NSLog(@"equal");

    }

    else if(i==1)

    {

       NSLog(@"first bigger");

    }

    else

    {

       NSLog(@"second bigger");

    }

    

    NSString *str12=@"123.12";

    NSLog(@"int %d",[str12 intValue]);

    NSLog(@"bool %d",[str12 boolValue]);

    NSLog(@"float %.2f",[str12 floatValue]);

    NSLog(@"double %.2f",[str12 doubleValue]);//保留小数点后两位

    

   NSString *str13=@"My story is the type of twists and turns or ups and downs of amelodrama";

    NSArray *s;

    s=[str13 componentsSeparatedByString:@" "];//@" "里面为空格,空格为分隔符

    NSLog(@"%@",s);

    

   returnYES;

}


输出结果:



原创粉丝点击