zoukankan      html  css  js  c++  java
  • 学习ios【2】Objective-C 数字和字符串

    一 数字

    1.使用Foundation.h可以直接导入所有的头文件。
    在XCode中,想查看某个方法帮助,可以将光标放在方法上,按住option键同时单击即可。
     
    2.数字对象
     
    objective-c有几种基本数据类型,int float long等,不过有些时候比如NSArray需要使用数字对象,而前面几种基本数据类型并不是对象,此时我们可以使用NSNumber.
     
    对于每种基本数据类型,类方法都可以为它创建一个NSNumber对象,并设定值。这些方法都是以numberWith开头,如numberWithInteger等。也可以使用实例方法将以前创建的NSNuber对象设置为指定的值,这些方法是以initWith开头,如initWithFloat。
     
    NSNumber类有很多方法,几种常见用法如下:
    • numberWith…:用初始值创建NSNumber对象,例如:numberWithInt:100;
    • integerValue:获取NSNumber对象的值,其它如charValue,floatValue等;
    • isEqualToNumber:比较两个NSNumber对象的数值是否相等,返回一个BOOL值。
    • compare:比较两个NSNumber对象数值的大小。返回结果有NSOrderedAscending,NSOrderedSame和NSOrderedDescending。
    • 最近objective-c语言扩展允许通过@表达式创建数字对象。如果@之后的值是一个表达式或者变量需要使用括号。
     
    #import <Foundation/Foundation.h>
    int main(int argc,const char * argv[])
    {
        @autoreleasepool {
            NSNumber *myNumber,*floatNumber,*intNumber;
           
            intNumber=[NSNumber numberWithInt:100];
            NSLog(@"%li",[intNumber integerValue]);//100
           
            floatNumber=[NSNumber numberWithFloat:100.00];
            NSLog(@"%f",[floatNumber floatValue]);//100.000000
           
            myNumber=[NSNumber numberWithChar:'A'];
            NSLog(@"%c",[myNumber charValue]);//A
           
            //判断两个数字是否相等
            if([intNumber isEqualToNumber:floatNumber]==YES)
            {
                NSLog(@"int number is equal to float number");//
            }
            else{
                NSLog(@"int number is not equal to float number");
            }
           
            //比较两个数字大小
            if([intNumber compare:myNumber]==NSOrderedAscending)
            {
                NSLog(@"int number is less than mynumber");
            }
            else{
                NSLog(@"int number is more than mynumber");//
            }
             
            //@语法
            intNumber=@200;
            NSLog(@"%li",[intNumber integerValue]);//200
        }
        return 0;
    }
     

    二 字符串

    1.常量字符串对象
    在字符串开头放一个@符号,就可以创建一个字符串常量对象。例如:@"Hello World!"
     
    2.decription
    NSLog(@"%@",a)中,%@既可以显示NSString对象,也可以显示其它对象。通过覆盖description方法,可以自定义对象的显示格式。
     
    Complex.h
    #import <Foundation/Foundation.h>
    @interface Complex : NSObject
    @property int real,imaginary;
    -(void)setReal:(int)r andImaginary:(int)m;
    @end
    Complex.m
    #import "Complex.h"
    @implementation Complex
    @synthesize real,imaginary;
    -(void)setReal:(int)r andImaginary:(int)m
    {
        self.real=r;
        self.imaginary=m;
    }
    //重写description,自定义输出格式
    -(NSString *)description
    {
        return [NSString stringWithFormat:@"%i + %ii",self.real,self.imaginary];
    }
    @end
     
    main.m
    #import <Foundation/Foundation.h>
    #import "Complex15.h"
    int main(int argc,const char * argv[])
    {
        @autoreleasepool {
            Complex15 *c=[[Complex15 alloc] init];
            [c setReal:1 andImaginary:2];
            NSLog(@"%@",c);//由<Complex15: 0x10010ffa0> 变为 1 + 2i
        }
        return 0;
    }
     
    2.不可变字符串NSString
    注意:不可变字符串对象表明它所引用的字符串对象的字符不可改变,但是可以重新为它们指定为其它的不可变字符串对象。
     
    NSString常用的方法:
    • length:获取字符串长度;
    • NSString stringWithString:复制字符串内容;
    • stringByAppendingString:连接两个字符串;
    • isEqualToString:比较两个字符串是否相等;
    • compare:字符串比较,常用于排序。比较结果为NSOrderedAssending,NSOrderedSame或NSOrderedDescending。
    • uppercaseString:字符串转换为大写;
    • lowercaseString:字符串转换为小写。
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            NSString *str1=@"This is String A";
            NSString *str2=@"This is String B";
            NSString *result;
            NSComparisonResult compareResult;
    
            // 字符串长度
            NSLog(@"length of str1 is %lu",[str1 length]);//length of str1 is 16
            //复制字符串
            result=[NSString stringWithString:str1];
            NSLog(@"Copy str1,result is: %@",result);//Copy str1,result is: This is String A
            //追加字符串
            str2=[str1 stringByAppendingString:str2];
            NSLog(@"str1 append str2 is:%@",str2);//str1 append str2 is:This is String AThis is String B
            //字符串是否相等
            if([str1 isEqualToString:result]){
                NSLog(@"str1==result");//
            }
            else{
                NSLog(@"str!=str2");
            }
            //比较字符串
            compareResult=[str1 compare:str2];
            if(compareResult==NSOrderedAscending){
                NSLog(@"str1<str2");//
            }else if(compareResult==NSOrderedSame){
                NSLog(@"str1=str2");
            }else{
                NSLog(@"str1>str2");
            }
            //大小写转换
            result=[str1 uppercaseString];
            NSLog(@"upper case:%@",result);//upper case:THIS IS STRING A
            result=[str1 lowercaseString];
            NSLog(@"lower case:%@",result);//lower case:this is string a
        }
        return 0;
    }
     
    子字符串操作:
    • substringToIndex:返回一个子串,截取从开头到指定索引数的字符,但是不包括这个字符。
    • substringFromIndex:返回一个子串,截取从指定位置到字符串结尾的字符。
    • substringWithRange:返回指定范围的字符,参数是NSRange类型。
    • rangeOfString:在字符串中查找子串,返回结果是NSRange类型,它是一个结构变量,不是对象变量。返回结果range.location表示子串起始位置,range.length表示子串长度。如果找不到指定子串,则location为NSNotFound。
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            NSString *str1=@"This is String A";
            NSString *result;
            NSRange range;
           
            //从开头取前N个字符
            result=[str1 substringToIndex:4];
            NSLog(@"前4个字符:%@",result);//前4个字符:This
            //取第N个字符到结尾
            result=[str1 substringFromIndex:8];
            NSLog(@"后8个字符:%@",result);//后8个字符:String A
            //提取第8到13个字符
            result=[[str1 substringFromIndex:8] substringToIndex:6];
            NSLog(@"第8到13个字符:%@",result);//第8到13个字符:String
            //另一种实现方式
            result=[str1 substringWithRange:NSMakeRange(8,6)];
            NSLog(@"第8到13个字符:%@",result);//第8到13个字符:String
            //查找子串
            range=[str1 rangeOfString:@"String"];
            NSLog(@"String子串位置%lu,长度是%lu",range.location,range.length);//String子串位置8,长度是6
            //查找不存在的子串
            range=[str1 rangeOfString:@"StringA"];
            if(range.location==NSNotFound){
                NSLog(@"未找到子串StringA");//
            }else{
                NSLog(@"子串StringA位置为%lu",range.location);
            }
         }
    }
    3.可变字符串NSMutableString
    NSMutalbleString是NSString的子类,用来创建可以更改字符的字符串对象。
     
    NSMutableString常用方法:
    insertString:atIndex :在指定位置插入字符串;
    appendString: 在字符串结尾插入字符串;
    deleteCharactersInRange:删除字符串中指定数目的字符。
    setString:直接设置可变字符串对象的内容;
     
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
            NSString *str=@"This is String A";
            NSMutableString *mstr;
            NSRange substr;
           
            mstr=[NSMutableString stringWithString:str];
            NSLog(@"%@",mstr);//This is String A
            //插入字符
            [mstr insertString:@" mutable" atIndex:7];
            NSLog(@"%@",mstr);//This is mutable String A
            //追加字符
            [mstr appendString:@" and String B"];
            NSLog(@"%@",mstr);//This is mutable String A and String B
            //删除子串
            [mstr deleteCharactersInRange:NSMakeRange(16, 8)];
            NSLog(@"%@",mstr);//This is mutable  and String B
            //查找子串并删除
            substr=[mstr rangeOfString:@"and String B"];
            if(substr.location!=NSNotFound){
                [mstr deleteCharactersInRange:substr];
                NSLog(@"%@",mstr);//This is mutable
            }
            //直接设置可变字符串内容
            [mstr setString:@"This is String."];
            NSLog(@"%@",mstr);//This is String.
            //替换子串
            [mstr replaceCharactersInRange:NSMakeRange(7, 1) withString:@" mutable "];
            NSLog(@"%@",mstr);//This is mutable String.
  • 相关阅读:
    Python3与Python2的区别(转载)
    Python——通过斐波那契数列来理解生成器
    Solr4.8.1与Tomcat7整合
    Solr配置与简单Demo
    lucene 4.4 demo
    企业级搜索引擎Solr使用入门指南
    Solr使用入门指南
    使用solr搭建你的全文检索
    Solr 1.3 安装步骤
    Lucene/Solr开发经验
  • 原文地址:https://www.cnblogs.com/janes/p/5467215.html
Copyright © 2011-2022 走看看