NSString 的常用操作,

來源:互聯網
上載者:User

NSString 的常用操作,

 

        NSString *testStr01=@"HelloWord";        NSString *testStr02=[testStr01 substringToIndex:5];//取頭(從頭到哪),to不包括下標內容        NSString *testStr03=[testStr01 substringFromIndex:5];//去尾(從哪到尾),from包括下標內容        NSLog(@"testStr02:%@  testStr03:%@",testStr02,testStr03);                NSRange r={4,3};//取中間,從哪長度是多少        NSString*testStr04=[testStr01 substringWithRange:r];        NSLog(@"testStr04:%@",testStr04);                                //字串的拼接        NSString *testStr05 = @"Hello";        //追加        NSString *testStr06 = [testStr05 stringByAppendingString:@"World"];        NSLog(@"testStr06:%@",testStr06);        //初始化        NSString *testStr07 = @"Hello";        NSString *testStr08 = @"World";        NSString *testStr09 = [NSString stringWithFormat:@"%@%@",testStr07,testStr08];        NSString *testStr10 = [NSString stringWithFormat:@"Hello%@",testStr08];        NSLog(@"testStr09:%@",testStr09);        NSLog(@"testStr10:%@",testStr10);                        //按指定格式(範圍)追加內容        NSString *testStr11 = @"Hello";        NSString *testStr12 = [testStr11 stringByAppendingFormat:@"%@%@",@"World",@"123"];        NSLog(@"testStr12:%@",testStr12);                        //替換        NSString *testStr13 = @"www.kyle.com.cn";        NSString *testStr14 = [testStr13 stringByReplacingCharactersInRange:NSMakeRange(4, 6) withString:@"163"];        NSLog(@"testStr14:%@",testStr14);                        //編碼集        //參數 檔案的路徑 不包括檔案名稱        NSString *path = @"/Users/apple/Desktop";        //path = [path stringByAppendingString:@"/test.txt"];        path = [path stringByAppendingString:@"/test.rtf"];        //把檔案中的內容 讀取到字串中        //NSString *str20 = [[NSString alloc]initWithContentsOfFile:path];                NSString *testStr15 = [[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];                NSLog(@"testStr15:%@",testStr15);                        NSString* use=@"fcp";        NSString* use1=@"fcp";                NSString*password=@"123";        NSString*password1=@"123";        if ([use isEqualToString:use1]&&[password isEqualToString:password1]) {            NSLog(@"登陸成功");        }        else{            NSLog(@"登陸失敗");        }            

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.