//
//  main.m
//  String
//
//  Created by neworigin on 14-9-17.
//  Copyright (c) 2014年 Damon. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{

    @autoreleasepool {
        //一、NSString
        /*----------------  创建字符串的方法 ----------------*/
        //1、创建常量字符串。
        NSString *astring1 = @"This is a String!";
        NSLog(@"astring1:%@",astring1);
        //2、创建空字符串,给予赋值。
        NSString *astring2 = [[NSString alloc] init];
        astring2 = @"This is a String!";
        [astring2 release];
        NSLog(@"astring2:%@",astring2);
        
        //3、在以上方法中,提升速度:initWithString方法
        NSString *astring3 = [[NSString alloc] initWithString:@"This is a String!"];
        NSLog(@"astring3:%@",astring3);
        [astring3 release];
        
        //4、用标准c创建字符串:initWithCString方法
        char *Cstring4 = "This is a String!";
        NSString *astring4 = [[NSString alloc]initWithCString:Cstring4 encoding:NSUTF8StringEncoding];
        NSLog(@"astring4:%@",astring4);
        [astring4 release];
        
        //5、创建格式化字符串:占位符(由一个%加一个字符组成)
        int i = 1;  int j = 2;
        NSString *astring5 = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%d.This is %i string!",i,j]];
        NSLog(@"astring5:%@",astring5);
        [astring5 release];
        
        //6、创建临时字符串
        NSString *astring6;
        astring6 = [NSString stringWithCString:"This is a temporary string" encoding:NSUTF8StringEncoding];
        NSLog(@"astring6:%@",astring6);
        
        /*----------写字符串到文件:writeToFile方法-----------*/
        NSString *astring7 = [[NSString alloc] initWithString:@"This is a String!"];
        NSLog(@"astring7:%@",astring7);
        NSString *path1 = @"/Users/neworigin/Desktop/txt1.txt";
        [astring7 writeToFile:path1 atomically:YES encoding:NSUTF8StringEncoding error:nil];
        [astring7 release];
        
        /*----从文件读取字符串:initWithContentsOfFile方法----*/
        
        NSString *path2 = @"/Users/neworigin/Desktop/txt1.txt";
        NSString *astring8 = [[NSString alloc]initWithContentsOfFile:path2 encoding:NSUTF8StringEncoding error:nil];
        NSLog(@"astring8:%@",astring8);
        [astring7 release];
        

        /*----------------比较两个字符串----------------*/
        //用C比较:strcmp函数
        char string1[] = "string!";
        char string2[] = "string!";
        if(strcmp(string1, string2) == 0)
        {
            NSLog(@"1");
        }
        
        //isEqualToString方法
        NSString *astring9 = @"This is a String!";
        NSString *astring10 = @"This is a String!";
        BOOL result = [astring9 isEqualToString:astring10];
        NSLog(@"result:%d",result);
        
        //compare方法(comparer返回的三种值)
        NSString *astring11 = @"This is a String!";
        NSString *astring12 = @"This is a String!!";
        NSComparisonResult result2 = [astring11 compare:astring12];
        NSLog(@"result2:%ld",result2);
        
        //NSCaseInsensitiveSearch:不区分大小写比较
   // NSLiteralSearch:进行完全比较,区分大小写
    //NSNumericSearch:比较字符串的字符个数,而不是字符值。
        /*-----------改变字符串的大小写------------*/
        NSString *string30 = @"A String";
        NSString *string31 = @"sTring";
        NSLog(@"string30:%@",[string30 uppercaseString]);//大写
        NSLog(@"string31:%@",[string31 lowercaseString]);//小写
        NSLog(@"string31:%@",[string31 capitalizedString]);//首字母大小
        
        /*----------------在串中搜索子串----------------*/
        NSString *string13 = @"This is a string";
        NSString *string14 = @"string";
        NSRange range = [string13 rangeOfString:string14];
        NSUInteger location = range.location;
        NSUInteger leight = range.length;
        NSString *astring15 = [[NSString alloc] initWithFormat:@"Location:%lu,Leight:%lu",(unsigned long)location,(unsigned long)leight];
        NSLog(@"astring15:%@",astring15);
        [astring15 release];
        
        /*----------------抽取子串 ----------------*/
        //-substringToIndex: 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
        NSString *string16 = @"This is a string";
        NSString *string17 =[string16 substringToIndex:3];
        NSLog(@"string17:%@",string17);
        //-substringFromIndex: 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
        NSString *string18 = @"This is a string";
        NSString *string19 = [string18 substringFromIndex:3];
        NSLog(@"string19:%@",string19);
        //-substringWithRange: //按照所给出的位置,长度,任意地从字符串中截取子串
        NSString *string20 = @"This is a string";
        NSString *string21 = [string20 substringWithRange:NSMakeRange(0, 4)];
        NSLog(@"string21:%@",string21);
        
        //扩展路径
        NSString *Path3 = @"/Users/neworigin/Desktop/txt1.txt";
        NSString *absolutePath = [Path3 stringByExpandingTildeInPath];
        NSLog(@"absolutePath:%@",absolutePath);
        NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
        //文件扩展名
        NSString *Path4 = @"/Users/neworigin/Desktop/txt1.txt";
        NSLog(@"Extension:%@",[Path4 pathExtension]);
        return 0;
    }
}

☟☟可点击下方广告支持一下☟☟

最后修改:1970 年 01 月 01 日
请我喝杯可乐,请随意打赏: ☞已打赏列表