当前位置:网站首页>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
边栏推荐
- 在VMware上安装Metasploitable2
- 从云计算到函数计算
- (Part of it is not understood, and the notes are not completed) [Graph Theory] Difference Constraints
- FormData上传二进制文件、对象、对象数组
- 责任链模式(Chain Of Responsibility)
- 敏捷、DevOps和嵌入式系统测试
- 有关 sql中的 concat()函数问题,如何拼接
- Find the largest n files
- PWA 踩坑 - 第一次加载页面后无法获取CacheStorage某些资源
- 实例027:递归输出
猜你喜欢

逆变器绝缘检测检测功能及软件实现

第06章 索引的数据结构【2.索引及调优篇】【MySQL高级】

【ROS基础】map、odom、base_link、laser 的理解 及其 tf 树的理解

spark架构

电商库存系统的防超卖和高并发扣减方案

System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志。不可 访问的日志: Security

实例028:递归求等差数列

FaceBook社媒营销高效转化技巧分享

2022.07.31(LC_6133_分组的最大数量)

吃透Chisel语言.31.Chisel进阶之通信状态机(三)——Ready-Valid接口:定义、时序和Chisel中的实现
随机推荐
【网络】IP、子网掩码
【机器学习】实验4布置:AAAI会议论文聚类分析
查找最大的n个文件
Agile, DevOps and Embedded Systems Testing
Redis 常用命令和基本数据结构(数据类型)
实例030:回文数
WebGPU 导入[2] - 核心概念与重要机制解读
入门opencv,欢笑快乐每一天
【机器学习】实验5布置:AAAI会议论文聚类分析
CSRF-跨站请求伪造-相关知识
MySQL-索引优化和查询优化
apt & apt-get命令
【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】
【请教】SQL语句按列1去重来计算列2之和
【机器学习】实验2布置:基于回归分析的大学综合得分预测
Splunk Filed Alias 字段改名
SQL执行顺序
【杂】pip换国内源教程及国内源地址
mysql 注入
CollectionUtil:一个函数式风格的集合工具