网站开发众包平台,南昌做网站建站的,绍兴seo网站优化,甲醇网络销售怎么找客源前言
本篇文章介绍OC的字符串
字符串常量
要使用Objective-C语言创建一个常量字符串对象#xff0c;需要在字符串开头放置 一个字符#xff0c;下面的语句创建了一个常量字符串
Programoning is fun NSLog
NSLog函数可以打印格式化对象#xff0c;类似于C语…前言
本篇文章介绍OC的字符串
字符串常量
要使用Objective-C语言创建一个常量字符串对象需要在字符串开头放置 一个字符下面的语句创建了一个常量字符串
Programoning is fun NSLog
NSLog函数可以打印格式化对象类似于C语言的printf 看下面的例子
int main(int argc, const char * argv[]) {autoreleasepool {NSString* a this is a string.;NSLog(%,a);}return 0;
}注意在使用NSLog打印对象的时候其实是打印对象调用description属性返回的字符串对象。
这是description属性的原型该属性是在NSObject类里边定义的。
property (readonly, copy) NSString *description;默认情况下该函数返回类的名称和当前对象的地址。 看下面代码我们定义一个类A
interface A : NSObject
end下面开始打印A的对象
int main(int argc, const char * argv[]) {autoreleasepool {A* a [[A alloc] init];NSLog(%,a);}return 0;
}返回结果
A: 0x600000008030现在我们重新给A添加description方法。
interface A : NSObject
-(NSString*)description;
end
implementation A
-(NSString*)description
{return this is class A.;
}
end再次打印发现输出结果已经变成
this is class A.#NSString NSString对象包含很多方法和属性下面列出常用的方法
NSString常用方法
类初始化方法
// 创建一个空字符串(instancetype)string;// 创建一个字符串的拷贝注意这两个字符串是深拷贝二者
// 并没有什么关系(instancetype)stringWithString:(NSString *)string NS_FORMAT_ARGUMENT(1);// 通过指定的format和参数argl ,arg2.arg3...
// 创建一个新字符串这是最常用的方式(instancetype)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);// 创建一个新字符串并将其设置为url的内容使用字符编码enc如果非零则返回error中的错误(nullable instancetype)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;// 创建一个新字符串并将其设置为path指定的文件的内容
// 使用字符编码enc如果非零则返回error中的错误(nullable instancetype)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;实例初始化方法
// 创建一个字符串的拷贝注意这两个字符串是深拷贝二者
// 并没有什么关系
- (instancetype)initWithString:(NSString *)aString NS_FORMAT_ARGUMENT(1);// 通过指定的format和参数argl ,arg2.arg3...
// 创建一个新字符串这是最常用的方式
- (instancetype)initWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);// 创建一个新字符串并将其设置为url的内容使用字符编码enc如果非零则返回error中的错误
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;// 将字符串设置为Path指定的文件内容
- (nullable instancetype)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;获取字符串的长度
-(NSUInteger)length;字符或字符串的查找
如果我们需要查找字符串中的特定字符或者特定字符串可以使用下面的方法
// 从头开始查询特定字符串A的位置
// 查询规则如下
// 1. 如果在字符串中找到A返回第一个匹配的位置和A的长度
// 2. 如果没有找到A匹配位置返回-1长度返回0
- (NSRange)rangeOfString:(NSString *)searchString;// 查询规则和上面的方法一样不过多了一个选项
// 一般我们用到的选项有
// 1. NSBackwardsSearch从后向前查询这个比较常用
// 2. NSCaseInsensitiveSearch不考虑字符串的大小写
- (NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;下面是一个根据文件全路径获取文件名称的例子
-(NSString*)getFileName:(NSString*)path
{NSString* fileName path;if([path containsString:/]){NSRange range [path rangeOfString:/ options:NSBackwardsSearch];fileName [path substringFromIndex:range.location1];}if([fileName containsString:.]){NSRange range [fileName rangeOfString:. options:NSBackwardsSearch];fileName [fileName substringToIndex: range.location];}return fileName;
}字符串的截取
从头开始截取
// 从头开始截取到to的位置
// 注意:
// 1. to是从0开始的索引
// 2. 不包含to的位置
- (NSString *)substringToIndex:(NSUInteger)to;看下面例子
NSString * s 0123456789;
NSLog(%,[s substringToIndex:3]);运行结果
012从某个位置开始
// 从from开始截取一直到最后
// 注意:
// 1. from是从0开始的索引
// 2. 包含from的位置
- (NSString *)substringFromIndex:(NSUInteger)from;看下面例子
NSString * s 0123456789;
NSLog(%,[s substringFromIndex:3]);运行结果
3456789范围截取
// 使用range表示的范围截取字符串
// 注意
// 截取包含range的开始位置
// 必须保证range在正确范围内否则会崩溃
- (NSString *)substringWithRange:(NSRange)range;看下面例子
NSString * s 0123456789;
NSRange range NSMakeRange(3, 4);
NSLog(%,[s substringWithRange:range]);运行结果
3456字符串比较
// 比较两个字符串
- (NSComparisonResult)compare:(NSString *)string;// 比较两个字符串忽略大小写
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;NSComparisonResult是一个枚举类型有三个值
NSOrderedAscending表示小于该值为-1NSOrderedSame表示等于该值为0NSOrderedDescending表示大于该值为1 对于
[A compare:B]具体的比较规则是这样的 从头开始比较A和B的每个字符
如果B已经到头了A还没有到头则AB结束如果A已经到头了B还没有到头则AB结束如果A和B都到头了则AB结束否则执行下一步。如果A的字符值比B小则AB结束如果A的字符值比B大则AB结束否则跳到第一步比较下个字符。
字符串首尾包含判断
// 测试字符串是否以str开始
- (BOOL)hasPrefix:(NSString *)str;// 测试字符串是否以str结束
- (BOOL)hasSuffix:(NSString *)str;字符串相等
// 比较两个字符串是否相等
- (BOOL)isEqualToString:(NSString *)aString;注意比较两个字符串内容是否相等的话千万别用用这个方法
看下面的例子
NSString * s 0123456789;
NSString * s1 01234567;
NSString * s2 [s1 stringByAppendingString:89];
if(s s2)
{NSLog(ss2);
}
else
{NSLog(s!s2);
}这个例子打印结果会是s!s2 如果我们用isEqualToString就会打印ss2
大小写转换
// 将字符串的所有字母变为大写
property (readonly, copy) NSString *uppercaseString;// 将字符串的所有字母变为小些
property (readonly, copy) NSString *lowercaseString;//将每个单词的首字母变为大写其余字母变为小写
property (readonly, copy) NSString *capitalizedString;与数字之间的转换
property (readonly) double doubleValue;
property (readonly) float floatValue;
property (readonly) int intValue;
property (readonly) NSInteger integerValue API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
property (readonly) long long longLongValue API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0));
property (readonly) BOOL boolValue API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0),NSMutableString
当我们使用NSString创建一个字符串的时候默认是一个不可变字符串也就是说无论是我们截取字符串或者对字符串进行大小写切换都会产生一个新的字符串看下面的例子
NSString * s 01234567890;
NSString * s1 [s stringByAppendingString:0];
NSString * s2 [s substringToIndex:3];
NSString * s3 [s substringFromIndex:3];
NSString * s4 [s uppercaseString];
NSLog(s %p,s);
NSLog(s %p,s1);
NSLog(s %p,s2);
NSLog(s %p,s3);
NSLog(s %p,s4);下面是一个打印结果
s 0x100004088
s 0x600000c00ba0
s 0xa747e51693e8bc7d
s 0xa7790060eba60aa5
s 0x600000c00bd0每个字符串都有一个新的地址。
NSMutableString是可变字符串是NSString的子类NSMutableString在内部使用可变数组保存字符数据下面是NSMutableString常用的方法
类初始化方法
// capacity是初始化字符串时分配的数组大小(NSMutableString *)stringWithCapacity:(NSUInteger)capacity;实例初始化方法
- (NSMutableString *)initWithCapacity:(NSUInteger)capacity;赋值
// 将字符串设置为aString
- (void)setString:(NSString *)aString;拼接
// 将字符串拼接到现在字符串的后面
- (void)appendString:(NSString *)aString;// 使用格式化字符串进行拼接
- (void)appendFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);删除
// 删除指定范围的字符串
- (void)deleteCharactersInRange:(NSRange)range;插入
// 在loc位置的前面插入字符串
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)loc;替换
// 用字符串aString替换现在字符串range区域
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)aString;// 使用字符串replacement替换所有的target
- (NSUInteger)replaceOccurrencesOfString:(NSString *)target withString:(NSString *)replacement options:(NSStringCompareOptions)options range:(NSRange)searchRange;综合实力
下面是一个综合的例子
NSMutableString* ms [NSMutableString stringWithCapacity:20];
NSLog(ms %,ms);
[ms setString:start::];
NSLog(ms %,ms);
[ms appendString:appendString,];
NSLog(ms %,ms);
[ms appendFormat:appendFormat%i,123];
NSLog(ms %,ms);
[ms deleteCharactersInRange:NSMakeRange(ms.length-3, 3)];
NSLog(ms %,ms);
[ms insertString:ecalper atIndex:0];
[ms insertString:replace atIndex:ms.length];
NSLog(ms %,ms);
[ms replaceCharactersInRange:NSMakeRange(0, 7) withString:replace];
NSLog(ms %,ms);
[ms replaceOccurrencesOfString:replace withString: options:0 range:NSMakeRange(0, ms.length)];
NSLog(ms %,ms);结果如下
ms
ms start::
ms start::appendString,
ms start::appendString,appendFormat123
ms start::appendString,appendFormat
ms ecalperstart::appendString,appendFormatreplace
ms replacestart::appendString,appendFormatreplace
ms start::appendString,appendFormat