iOS基础一 NSString
来源:互联网 发布:java中嵌入javascript 编辑:程序博客网 时间:2024/05/21 06:34
NSString *string = @"Progamming is funny"; NSLog(@"%@",string); NSString *str1 = @"this is string A"; NSString *str2 = @"this is string B"; NSString *res; NSComparisonResult comparionResult; //count the number of characters NSLog(@"Length of str1: %lu", [str1 length]); //copy one string to another res = [NSString stringWithString:str1]; NSLog(@"copy: %@",res); //copy one string to the end of another str2 = [str1 stringByAppendingString:str2]; NSLog(@"Conactenation: %@",str2); //Test if 2 strings are equal if([str1 isEqualToString:str2] == YES) NSLog(@"str1 == str2"); else NSLog(@"str1 != str2"); //Test if one string is <, ==, or > than another comparionResult = [str1 compare: str2]; if(comparionResult == NSOrderedAscending) NSLog(@"str1 < str2"); else if(comparionResult == NSOrderedSame) NSLog(@"str1 == str2"); else NSLog(@"str1 > str2"); //conver a string to upperCase res = [str1 uppercaseString]; NSLog(@"%@",res); //conver a string to lowerCase res = [str1 lowercaseString]; NSLog(@"%@", res); NSRange subRange; //Extact first 3 chars from string res = [str1 substringToIndex:3]; NSLog(@"First 3 chars of str1: %@", res); //Extact chars to end of string starting at index 5 res = [str1 substringFromIndex:5]; NSLog(@"chars from index 5 of str1: %@",res); //Extact char from index 8 through 13 (6 chars) res = [[str1 substringFromIndex:8] substringToIndex:6]; NSLog(@"chars from index 8 through 13: %@",res); //An easier way to do the same thing res = [str1 substringWithRange:NSMakeRange(8, 6)]; NSLog(@"chars from index 8 through 13: %@",res); //locate one string inside another subRange = [str1 rangeOfString:@"string A"]; NSLog(@"string is at index %lu, length is %lu",subRange.location,subRange.length); subRange = [str1 rangeOfString:@"string B"]; if(subRange.location == NSNotFound) NSLog(@"string not found"); else NSLog(@"string is at index %lu, length is %lu",subRange.location,subRange.length); ///mutable NSString *search,*replace; NSMutableString *mstr; NSRange subString; //create mutable string from nonmutable mstr = [NSMutableString stringWithString:str1]; NSLog(@"%@",mstr); //Insert characters [mstr insertString:@" mutable" atIndex:7]; NSLog(@"%@",mstr); //Effective contacentation if insert at end [mstr insertString:@" and string B" atIndex:[mstr length]]; NSLog(@"%@",mstr); //or can user appendString directly [mstr appendString:@" and string C"]; NSLog(@"%@",mstr); //delete substring based on range [mstr deleteCharactersInRange:NSMakeRange(16, 13)]; NSLog(@"%@",mstr); //Find range first and the use it for delection subString = [mstr rangeOfString:@" string B and"]; if(subString.location != NSNotFound){ [mstr deleteCharactersInRange:subString]; NSLog(@"%@",mstr); } //set the mutable string [mstr setString:@"This is string A"]; NSLog(@"%@",mstr); //replace a range of chars with another [mstr replaceCharactersInRange:NSMakeRange(8, 8) withString:@"a mutable string"]; NSLog(@"%@",mstr); //search and replace search = @"This is "; replace = @"An example of "; subString = [mstr rangeOfString:search]; if(subString.location != NSNotFound){ [mstr replaceCharactersInRange:subString withString:replace]; NSLog(@"%@",mstr); } //search and replace all occurrences search = @"a"; replace = @"X"; subString = [mstr rangeOfString:search]; while (subString.location != NSNotFound) { [mstr replaceCharactersInRange:subString withString:replace]; subString = [mstr rangeOfString:search]; } NSLog(@"%@",mstr);
0 0
- iOS基础一 NSString
- 【iOS 基础】NSString 类
- iOS 基础类解析 - NSString
- IOS基础 NSString 和 NSMutableString
- ios-基础之【6】-NSString
- iOS基础:NSString常用方法
- iOS NSString的讲解(一)
- iOS开发系列之Objective-C基础:NSString字符串类型(一)
- iOS 基础类解析 - NSString、NSMutableString
- iOS- 基础 [NSString boolValue]结果分析
- IOS NSString
- IOS NSString
- ios NSString
- iOS-NSString ? :
- ios NSString
- IOS-Nsstring
- NSString ios
- 【IOS开发基础】之判断NSString为纯数字
- QVGA/HVGA/WVGA/FWVGA分辨率屏含义及大小
- 线性代数(四十一) : 伴随矩阵
- C#导入Excel数据的方式(两种)
- javaweb之自定义标签——带属性的标签
- 签到 3.24
- iOS基础一 NSString
- Spring 的优秀工具类盘点
- 蓄水池抽样(Reservoir Sampling)分析
- 开启 Spring & Spring MVC 之旅
- 线性代数(五十一) : 自伴随映射与二次型
- python challenge_4
- 关于oracle隐含参数 总结
- 用MyEclipse8.5及以上版本创建webservice实例(入门级超简单)
- 专业毕业设计制作,欲购从速(计算机方向,java,c++。。)