当前位置:网站首页>OC-NSString
OC-NSString
2022-08-02 07:49:00 【Peng classmate her deskmate】
Foundation/NSString.h
String constants are called from the beginning of execution of the program to the termination of the programreleaseNeither method nor garbage collection can release
NSStringRepresents an immutable string object It cannot be changed by additions and deletions
String used internallyUnicode编码
NSStringin a cluster-like manner(class cluster)Think of it as a factory class
定义方式
NSString *a = @"abc";
The compiler will concatenate strings separated by spaces
拼接字符串
stringByAppendingFormat Multiple strings can be concatenated
NSString *a = @"abc";
NSString *c = @"zzzz";
NSString *b = [a stringByAppendingFormat:@"第一个参数: %@ 第二个参数%@",@12,c];//abc第一个参数: 12 第二个参数zzzz
-(NSUInteger)length 获取长度
返回字符串中UnicodeThe number of encoded characters The return value of this function cannot be used to calculate the number of bytes required Because it returns the number of characters But you don't know how many bytes a character occupies
componentsJoinedByString Concatenate strings by specifying symbols
NSArray* array = @[@"a",@"b",@"c"];
NSString *string = [array componentsJoinedByString:@"-"];
NSLog(@"%@",string);
查找字符串
rangeOfString //从stringFind the string inside and return itrange形式变量
找到了 就返回{location,length}
没找到 返回{NSNotFound,0}
NSString *string = @"0123456789abc";
NSRange range = [string rangeOfString:@"34"];
NSLog(@"@",NSStringFromRange(range));//{3,2} 第三个位置 两个字符
NSRange range = [string rangeOfString:@"Ab" options:NSCaseInsensitiveSerch];//添加参数 This is case sensitive
分割字符串
componentsSeparatedByString Split by the specified symbol
NSString *string = @"sbcd-efht-defagfds";
NSArray *array = [string componentsSeparatedByString:@"-"];
NSLog(@"array = %@",array);//array = (sbcd,efht,defagfds)
substringWithRange 通过range分割字符串
NSString *string = @"0123456789abc"
NSRange range ={
3,8};
NSString *newString = [string substringWithRange:range];//从第3Bits start to take eight 3456789a
删除字符串
stringByTrimmingCharactersInSet: 删除特殊符号 Commonly used spaces
NSString * string = @" abcd ";
string = [string stringByTrimmingCharactersInSet:NSCharacterSet whitespaceCharacterSet];//abcd whitespaceCharacterSet是空格
替换字符串
stringByReplacingOccurrencesOfString:withString: 将指定字符串a替换成指定字符串b
NSSstring string = @“lixun”;
NSString *newString = [string stringByReplacingOccurrencesOfString:@"li"withString:@“liu”];将li字符串替换成liu
stringByReplacingCharactersInRange:withString:
NSString * string = @“aaaaaaannnnnnn”;
NSRange range = {4,3};
string = [string stringByReplacingCharactersInRange:range withString:@“qqqqqqqq”];//Indicates that the fourth position will be3个字符替换成qqqqqqqqq //aaaqqqqqqqqannnnnnn
字符串比较
NSString *string1 = @“abc”;
NSString *string2 = @"ABC";
NSComparisonResult rs = [string1 compare:string2];
if(rs == NSOrderedAscending){
NSLog(@"string1<string2")};
else if(rs == NSOrderedSame){
NSLog(@"string1 = string2")};
else{
NSLog(@"string1>string2")};
关于UTF-8的解码方式
因为ASCIISimply using a byte to represent a character causes the text of many countries to be unable to be represented
所以发明了unicode编码 unicodeThere are several ways of encoding UTF-8 UTF-16 UTF-32
UTF-8The biggest feature is the variable length 用1-4个字节表示
1.When only represented by a single byte 第一位设为0 后面7位对应这个字符的Unicode码
2.对于需要使用Ncharacter to represent(N>1)第一个字节的前N位都设为1 第N+1位设为0
The first two bits of the remaining bytes are set10
So if the first one is0It means that one byte corresponds to one character 如果第一位是1 Then see how many consecutive1It indicates how many bytes a character occupies
0xxxxxxx
110xxxxx 10xxxxxx
1110xxxx 10xxxxxx 10xxxxxx
11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
边栏推荐
- 实例031:字母识词
- MQ带来的一些问题、及解决方案
- LeetCode Algorithm 1374. 生成每种字符都是奇数个的字符串
- 【机器学习】实验1布置:基于决策树的英雄联盟游戏胜负预测
- spark架构
- 自然语言处理 文本预处理(下)(张量表示、文本数据分析、文本特征处理等)
- Link with Game Glitch(spfa判负环)
- 【红队】ATT&CK - 创建或修改系统进程实现持久化(更新ing)
- 论文《Deep Multifaceted Transformers for Multi-objective Ranking in Large-Scale E-commerce Recommender》
- Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)
猜你喜欢
随机推荐
C#重点问题之Struct和Class的异同
【机器学习】实验2布置:基于回归分析的大学综合得分预测
WebGPU 导入[2] - 核心概念与重要机制解读
解决Pytorch模型在Gunicorn部署无法运行或者超时问题
查看端口号占用
【网络】IP、子网掩码
Go implements distributed locks
sql 远程访问链接服务器
【暑期每日一题】洛谷 P3156 【深基15.例1】询问学号
CSRF-跨站请求伪造-相关知识
OC - NSSet (set)
【杂】pip换国内源教程及国内源地址
主流定时任务解决方案全横评
【机器学习】实验6布置:基于集成学习的Amazon用户评论质量预测
regular expression
实例030:回文数
“蔚来杯“2022牛客暑期多校训练营4,签到题NDKHL
MySQL-慢查询日志
Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)
MPLS的相关技术









