OC字串基本操作,oc基本操作

來源:互聯網
上載者:User

OC字串基本操作,oc基本操作

不可變的字串的修改方法有傳回值(重新指向新的字串地址)

可變的字串的修改方法沒有傳回值(修改字串本身)

/*

 NSString 不可變的字串

 */

// 1.建立字串對象

// 使用初始化方法建立對象

NSString *string1 = [[NSString alloc] initWithFormat:@"hello, world"];

 

// 使用類方法

NSString *string2 = [NSString stringWithFormat:@"hello, world"];

 

// 使用字面量

NSString *string3 = @"hello, kitty";

 

 

// 2.擷取字串的長度

NSLog(@"string1length = %ld", string1.length);

NSLog(@"string1length = %ld", [string1 length]);

 

 

// 3.擷取指定位置的字元

unichar ch = [string2 characterAtIndex:3];

NSLog(@"ch = %c", ch);

 

 

// 4.判斷字串的首碼和尾碼

NSString *string4 = @"www.歐巴.png";

BOOL isHasSuffix = [string4 hasSuffix:@".png"];

BOOL isHasPrefix = [string4 hasPrefix:@"www."];

NSLog(@"isHasSuffix = %d, isHasPrefix = %d", isHasSuffix, isHasPrefix);

 

  

// 5.尋找某一個字串在另一個字串中的範圍

NSString *string5 = @"大風起兮雲飛揚";

NSString *string6 = @"起兮";

// NSRange 一個表示範圍的結構體 包含兩個部分

// location表示起始位置, length表示長度

NSRange range = [string5 rangeOfString:string6];

NSLog(@"location = %ld, length = %ld", range.location, range.length);

 

 

// 6.字串截取

NSString *string7 = @"hello world kitty";

 

// 通過指定範圍截取字串

NSRange range1 = NSMakeRange(0, 5);

NSString *subStr1 = [string7 substringWithRange:range1];

NSLog(@"%@", subStr1);

 

// 從頭開始,截取一定長度

NSString *subStr2 = [string7 substringToIndex:5];

NSLog(@"%@", subStr2);

 

// 從指定位置,向後截取,包含指定位置的下標位置

NSString *subStr3 = [string7 substringFromIndex:string7.length - 5];

NSLog(@"%@", subStr3);

 

 

// 7.字串拼接

NSString *string8 = @"nana";

NSString *string9 = @"liuaoran";

NSString *love = [string8 stringByAppendingString:string9];

//NSString *love1 = [NSString stringWithFormat:@"%@%@",string8, string9];

NSLog(@"love = %@", love);

 

 

// 8.字串替換

NSString *string10 = @"李易峰";

NSString *string11 = [string10 stringByReplacingOccurrencesOfString:string10 withString:@"趙的住"];

NSLog(@"%@", string11);

 

 

// 9.字串比較

NSString *string12 = @"wangcai";

NSString *string13 = @"xiaoqiang";

NSInteger result = [string12 compare:string13];

NSLog(@"%ld", result);

 

// 判斷兩個字串是否相等

BOOL isEqual = [string12 isEqualToString:string13];

NSLog(@"%d", isEqual);

 

 

// 10.字串和數實值型別的轉換

NSString *string14 = @"123";

NSInteger value1 = [string14 integerValue];

NSLog(@"%ld", value1);

 

 

// 11.字串大小寫之間的轉換

NSString *string15 = @"da feng qi xi yun fei yang";

NSString *string16 = @"Da Feng Qi Xi Yun Fei Yang";

 

// 首字母大寫

NSString *str1 = [string15 capitalizedString];

NSLog(@"%@", str1);

 

// 全部大寫

NSString *str2 = [string15 uppercaseString];

NSLog(@"%@", str2);

 

// 全部小寫

NSString *str3 = [string16 lowercaseString];

NSLog(@"%@", str3);

 

 

 

/**

 *  NSMutableString

 */

// 1.建立對象

// 初始化方法

NSMutableString *mStr1 = [[NSMutableString alloc] initWithFormat:@"hello, world"];

 

// 類方法

NSMutableString *mStr2 = [NSMutableString stringWithFormat:@"hello kitty"];

 

// 不能使用字面量的方式建立,因為使用字面量建立的都是不可變的字串

 

 

// 2.拼接字串

NSMutableString *mStr3 = [NSMutableString stringWithFormat:@"衛莊"];

[mStr3 appendString:@"是蓋聶的師弟"];

NSLog(@"%@", mStr3);

 

  

// 3.插入字串

[mStr2 insertString:@"," atIndex:5];

NSLog(@"%@", mStr2);

 

 

// 4.刪除字串

[mStr1 deleteCharactersInRange:NSMakeRange(5, 1)];

NSLog(@"%@", mStr1);

 

 

// 5.字串替換

NSMutableString *mStr4 = [NSMutableString stringWithFormat:@"天明和少羽是好哥們"];

[mStr4 replaceCharactersInRange:NSMakeRange(3, 3) withString:@"月兒"];

NSLog(@"%@", mStr4);

 

 

// 6.重設字串

[mStr4 setString:@"星魂大人"];

NSLog(@"%@", mStr4);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.