OC Foundation框架—字符串

时间:2023-05-08 20:02:13

一、Foundation框架中一些常用的类

字符串型:

NSString:不可变字符串

NSMutableString:可变字符串

集合型:

1)

NSArray:OC不可变数组

NSMutableArray:可变数组

2)

NSSet:

NSMutableSet:

3)

NSDictiorary

NSMutableDictiorary

其它:

NSDate

NSObject

二、NSString和NSMutableString的使用与注意

(一)6种创建字符串的形式

OC Foundation框架—字符串

(二)使用注意

(1)字符串的导入导出

OC Foundation框架—字符串

(2)类方法

OC Foundation框架—字符串

(三)NSMutableString的使用与注意

OC Foundation框架—字符串

(四)URL补充内容

Url:资源路径

格式:协议头:/路径

网络路径协议头:http

本地文件以file为协议头

ftp为协议头,说明资源是ftp服务器上的。

(四)字符串操作补充

 #import <Foundation/Foundation.h>

 int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
// NSLog(@"Hello, World!"); /*
不可变字符串
1.创建字符串
实例方法(3种)
类方法(3种)
2.不可变字符串:
统计长度(2种)
索引提取 (3种)
比较 (3种)
查照字符串位置(1种)
判断前后缀
转换数字
大小写转换
字符串提取 可变字符串
1.创建可变字符串
2.可变字符串的操作
增(3种)删(1种)改(1种)查 */ //实例方法(3种)
NSString *str1 = @"";
NSString *str2 = [[NSString alloc] initWithFormat:@"%d4567",];
// NSString *str3 = [[NSString alloc] initWithString:str2];//传的是对象 //类方法(3种)
NSString *str4 = [NSString stringWithFormat:@"Abc%@eFg",@"d"];
// NSString *str5 = [NSString stringWithString:str4]; //统计长度(2种)
NSUInteger length = [str1 length];
NSUInteger length1 = str1.length;
NSLog(@"%lu %lu",length,length1); //索引提取 (3种)
NSString *newStr1 = [str1 substringFromIndex:];
NSLog(@"%@",newStr1);
NSString *newStr2 = [str1 substringToIndex:];
NSLog(@"%@",newStr2);
NSString *newStr3 = [str1 substringWithRange:NSMakeRange(, )];
NSLog(@"%@",newStr3);

NSString *string = [[NSString alloc] initWithFormat:@"%@",@"qwert"];

unichar ch =[string characterAtIndex:2];//提取字符串中的一个字符变成字符

NSLog(@"%c",ch);


         //比较 (3种)
if ([str1 isEqualToString:str2]) {
NSLog(@"相同");
}else
NSLog(@"不相同"); if ([str1 compare:str2] == NSOrderedAscending) {
NSLog(@"str1>str2");
}
else if ([str1 compare:str2] == NSOrderedDescending)
NSLog(@"str1<str2");
else if ([str1 compare:str2] == NSOrderedSame)
NSLog(@"=="); if ([str1 caseInsensitiveCompare:str2] == NSOrderedAscending) {
NSLog(@"str1>str2");
}
else if ([str1 caseInsensitiveCompare:str2] == NSOrderedDescending)
NSLog(@"str1<str2");
else if ([str1 caseInsensitiveCompare:str2] ==NSOrderedSame)
NSLog(@"=="); // 查找字符串位置
NSRange range = [str1 rangeOfString:str2];
if (range.location != NSNotFound) {
NSLog(@"location:%lu length:%lu",range.location,range.length); } else {
NSLog(@"没找到");
} //判断前后缀(2种)
BOOL flag = [str4 hasPrefix:@"abc"];
NSLog(@"%d",flag); BOOL anotherFlag = [str4 hasSuffix:@"fg"];
NSLog(@"%d",anotherFlag); //转换数字(4种)
int number = [str1 intValue];
NSLog(@"%d",number); NSInteger number1 = [str1 intValue];
NSLog(@"%ld",number1); NSString *str6 = @"1234567.89";
double number2 = [str6 doubleValue];
NSLog(@"%lf",number2); //大小写转换
NSString *upStr = [str4 uppercaseString];
NSLog(@"%@",upStr);
NSString *lowStr = [str4 lowercaseString];
NSLog(@"%@",lowStr);
NSString *tempStr = [str4 capitalizedString];
NSLog(@"%@",tempStr); // 可变字符串
// 实例方法
NSMutableString *muStr1 = [[NSMutableString alloc]
initWithFormat:@"1%d3",]; NSMutableString *muStr2 = [[NSMutableString alloc]
initWithString:@""]; NSMutableString *muStr3 = [[NSMutableString alloc]
initWithCapacity:]; // 类方法
NSMutableString *muStr4 = [NSMutableString stringWithFormat:@"4%d6",]; NSMutableString *muStr5 = [NSMutableString stringWithString:@""]; NSMutableString *muStr6 = [NSMutableString stringWithCapacity:]; //增(3种)
[muStr1 appendFormat:@"%d",];
NSLog(@"%@",muStr1); [muStr2 appendString:@""];
NSLog(@"%@",muStr2); [muStr3 insertString:@"abcdefghijklmn" atIndex:];
NSLog(@"%@",muStr3); [muStr4 insertString:@"abcdefghijklmn" atIndex:];
NSLog(@"%@",muStr4); //删除
[muStr5 deleteCharactersInRange:NSMakeRange(, )];
NSLog(@"%@",muStr5); //改
[muStr6 replaceCharactersInRange:NSMakeRange(, ) withString:@""];
NSLog(@"%@",muStr6); [muStr5 replaceCharactersInRange:NSMakeRange(, ) withString:@"abcd"];
NSLog(@"%@",muStr5); }
return ;
}