当前位置:网站首页>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
边栏推荐
猜你喜欢

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

【图像隐藏】基于matlab混合DWT-HD-SVD数字图像水印方法技术【含Matlab源码 2007期】

企业实训复现指导手册——基于华为ModelArts平台的OpenPose模型的训练和推理、基于关键点数据实现对攀爬和翻越护栏两种行为的识别、并完成在图片中只标注发生行为的人

实例030:回文数

regular expression

根据一个字段的内容去更新另一个字段的数据,这样的sql语句该怎么样书写

【故障诊断分析】基于matlab FFT轴承故障诊断(包络谱)【含Matlab源码 2002期】

LeetCode 2360. The longest cycle in a graph

【请教】SQL语句按列1去重来计算列2之和

张驰课堂:六西格玛测量系统的误差分析与判定
随机推荐
【机器学习】实验6布置:基于集成学习的Amazon用户评论质量预测
逆变器绝缘检测检测功能及软件实现
【机器学习】课程设计布置:某闯关类手游用户流失预测
【心电信号】基于matlab心率检测【含Matlab源码 1993期】
实例026:递归求阶乘
电商库存系统的防超卖和高并发扣减方案
Splunk Filed extraction field interception
根据一个字段的内容去更新另一个字段的数据,这样的sql语句该怎么样书写
【请教】SQL语句按列1去重来计算列2之和
分离轴定理SAT凸多边形精确碰撞检测
【论文精读】Geometric Structure Preserving Warp for Natural Image Stitching
【机器学习】实验5布置:AAAI会议论文聚类分析
责任链模式(Chain Of Responsibility)
Analysis of GCC compiler technology
【CNN回归预测】基于matlab卷积神经网络CNN数据回归预测【含Matlab源码 2003期】
MQ带来的一些问题、及解决方案
【ROS基础】rosbag 的使用方法
LeetCode Algorithm 1374. 生成每种字符都是奇数个的字符串
Splunk Filed extraction 字段截取
JS初识高阶函数和函数柯里化