OC中NSString类的常用方法

1,字符串的简单用法:
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    //创建常量字符串
    NSString* str=@"This is a string";
    
    //1,获取字符串的长度</span>
    NSUInteger len=[str length];
    NSLog(@"length=%lu", len);   //result:16
    
    //2,获取下标为1的字符,用unichar接收</span>
    unichar c=[str characterAtIndex:1];
    NSLog(@"%c", c);     //result:h
    
    NSLog(@"%lu", sizeof(unichar));  //result:2
    
    //3,initWithString初始化字符串</span>
    str=[[NSString alloc] 
         initWithString@"Hello World"];
    NSLog(@"%@", str);           //result:hello world
    [str release];
    
    //4,用标准c字符串创建oc字符串 :initWithCString 方法
    str=[[NSString alloc] 
         initWithUTF8String:"This is c string"];
    NSLog(@"%@", str);        //result:This is c string
    printf("%s\n", [str UTF8String]);
    
    [str release];
    
    str=[NSString stringWithString:@"Hello iOS"];
    NSLog(@"%@", str);         //result Hello iOS
    
    int age=30;
    //5,创建格式化字符串。
    str=[[NSString alloc] 
         initWithFormat:@"age=%d", age];
    NSLog(@"%@", str);
    [str release];
    //6,stringWithFormat也可用于拼接字符串</span>
    str=[NSString stringWithFormat:@"1+1=%d", 1+1];
    
    NSLog(@"%@", str);
    
    
    [pool drain];
    return 0;
}
2,从文件中读取字符串
#import <Foundation/Foundation.h>

#define PATH "/Users/kj/Desktop/dict.txt"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSError* error=nil;
    //下面也可用 NSString* path=[NSString stringWithUTF8String:PATH];就不用手动释放path了
    NSString* path=[[NSString alloc] initWithUTF8String:PATH];
    
    /*
     下面也可用
     NSString* fileContentString=
                     [[NSString alloc] 
                     initWithContentsOfFile:path 
                     encoding:NSUTF8StringEncoding 
                     error:&error];
     
     但是不要忘了
     [fileContentString release];
     */
    NSString* fileContentString=[NSString
                stringWithContentsOfFile:path
                encoding:NSUTF8StringEncoding
                error:&error];   //如果创建失败,error改变指针指向,指向内部创建的一个NSerror对象
    
    [path release];
    
    if (nil!=error) {
        NSLog(@"%@", error);
        exit(-1);
    }
    NSLog(@"%@", fileContentString);
    
    [pool drain];
    return 0;
}


3,写入文件到字符串
 
NSString *astring = [[NSString alloc]
                             initWithString:@"This is a String!"];

    NSLog(@"astring:%@",astring);
    NSString *path = @"astring.text";
    [astring writeToFile: path atomically: YES];
    [astring release];

4,重写类描述方法



#import <Foundation/Foundation.h>

@interface Person : NSObject
@end

@implementation Person
#if 1
-(NSString*)description
{
    return @"This is Person's object";
}
#endif 
@end



int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

 
    Person* p=[[Person alloc] init];
    NSLog(@"%@", p);
    [p release];
    
    [pool drain];
    return 0;
}

重写了类描述方法,我们用NSLog(@"%@", p );就会调用我们自己的方法。
调试的时候覆写类描述方法比较有用
5,字符串的比较

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString* str1=@"Hello World";
    NSString* str2=@"hello World";
    
    if([str1 isEqualToString:str2])
    {
        NSLog(@"YES");
    }
    else 
    {
        NSLog(@"NO");
    }
    //compare方法对应的返回值为enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};</span>
    NSComparisonResult ret=[str1 compare:str2];
    if (NSOrderedAscending==ret) {
        NSLog(@"%@<%@", str1, str2);
    }
    
    if (NSOrderedSame==ret) {
        NSLog(@"%@=%@", str1, str2);
    }
    
    if (NSOrderedDescending==ret) {
        NSLog(@"%@>%@", str1, str2);
    }
    

    [pool drain];
    return 0;
}

6,字符串的截取
 

#import <Foundation/Foundation.h>

NSString* MyStringFromRange(NSRange range)
{
    NSString* str=[NSString 
        stringWithFormat:@"{%lu, %lu}", 
                   range.location, range.length];
    return str;
}
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   
    NSString* str1=@"this is a string";
    NSString* str2=@"string";
    NSRange range;
    //判断一个字符串中是否包含有另外一个字符串,并返回str2再str1中得位置和长度
    range=[str1 rangeOfString:str2];
    
    if(range.location!=NSNotFound) //
    {
#if 0
        NSLog(@"loc:%lu, len:%lu",
              range.location, range.length );
#endif
        //NSLog(@"%@", NSStringFromRange(range));
        NSLog(@"%@", MyStringFromRange(range));
    }
    
    //iPhone, iPad, iTV, iOS, iMac
    str1=@"Hello, iOS";
    //判断字符串中是否包含前缀或着后缀。
    [str1 hasPrefix:@"Hello"] == YES ?NSLog(@"YES Hello") : NSLog(@"NO");
    [str1 hasSuffix:@"iOS"] == YES ?NSLog(@"YES iOS") : NSLog(@"NO");
    
    //截取字符串
    //-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
    //-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
    //-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串</span>
    str2=[str1 substringFromIndex:7];
    NSLog(@"%@", str2);
    NSLog(@"%@", str1);
    
    str2=[str1 substringToIndex:5];
    NSLog(@"%@", str2);
    
    
    //range=(NSRange){3, 2};
    range=NSMakeRange(3, 2);
    str2=[str1 substringWithRange:range];
    NSLog(@"%@", str2);
    
    [pool drain];
    return 0;
}

 


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值