nsstring

NSString字符串截取方法

南楼画角 提交于 2020-02-02 07:58:55
1. 字符串 1> 字符串比较 NSString *a = @“hello”; NSString *b = [NSString stringWithFormat:@hello”]; if (a == b){ nslog(@“a==b”); } if ([a isEqualToString: b]){ nslog(@“a isEqualToString b”); } == 比较变量中保存的数值 ( 地址 ) 速度快 内容同 , 可能地址不同 ( 常量区 , 堆区 ) isEqualTo 比较字符串 非常耗时 2> 字符串截取 截取字符串 ”20 | http://www.baidu.com” 中 ,”|” 字符前面和后面的数据 , 分别输出它们 。 NSString * str = @"20 | http://www.baidu.com "; NSArray *array = [str componentsSeparatedByString:@"|"]; // 这是分别输出的截取后的字符串 for (int i = 0; i<[array count]; ++i) { NSLog(@"%d=%@",i,[array objectAtIndex:i]); } 1.定义一个字符串a, 截取a 的某一个部分,复制给b, b必须是int型 NSString *a = @"1.2.30";

NSString截取字符串

╄→尐↘猪︶ㄣ 提交于 2020-02-02 06:38:09
一、带节点的字符串,如@"<p>讨厌的节点<br/></p>"我们只想要中间的中文 NSString *string1 = @"<p>讨厌的节点<br/></p>"; /*此处将不想要的字符全部放进characterSet1中,不需另外加逗号或空格之类的,除非字符串中有你想要去除的空格,此处< p /等都是单独存在,不作为整个字符*/ NSCharacterSet *characterSet1 = [NSCharacterSet characterSetWithCharactersInString:@"<p/brh>"]; // 将string1按characterSet1中的元素分割成数组 NSArray *array1 = [string1 componentsSeparatedByCharactersInSet:characterSet1]; NSLog(@"array = %@",array1); for(NSString *string1 in array1) { if ([string1 length]>0) { // 此处string即为中文字符串 NSLog(@"string = %@",string1); } } 打印结果: 2013-05-31 10:55:34.017 string[17634:303] 来源: https://www.cnblogs.com

NSString截取字符串

别来无恙 提交于 2020-02-02 06:03:27
NSString 是经常会用到的,很多时候需要对字符串进行一些处理,本文简单介绍字符串截取操作: 比如: 1.定义一个字符串a, 截取a的某一个部分(子串) NSString *a = @"1.2.30"; NSString * b = [a substringWithRange :NSMakeRange ( 4 , 2 ) ]; NSLog( @"a:%@ \n",a ); NSLog( @"b:%@",b); Output : 2011-07-05 11:49:08.170 Q[4005:207] a:1.2.30 2011-07-05 11:49:08.172 Q[4005:207] b:30 解析如下: substringWithRange: 专门截取字符串的一个子串 NSMakeRange ( 4 , 2 ) 从第4个字符开始截取,长度为2个字符,(字符串都是从第0个字符开始数的哦~!) b = [a intValue]; 将 a 转换为 整数型 b = [a floatValue]; 将 a 转换为 小数型 b = [a boolValue]; 将 a 转换为 布尔型(true / false) b = [a integerValue]; 将 a 转换为 整数型 b = [a longLongValue ]; 将 a 转换为 长整型 2。 字符串截取到第n位

截取NSString字符串

跟風遠走 提交于 2020-02-02 05:07:42
NSString类中提供了这样三个方法用于获取子字符串: – substringFromIndex: – substringWithRange: – substringToIndex : 具体的使用见下面代码即可知道。 NSString *str = @"12345"; // NSString *subString0 = [str substringFromIndex:-1]; //会放生越界错误 NSString *subString1 = [str substringFromIndex:0]; //@"12345" NSString *subString2 = [str substringFromIndex:1]; //@"2345" NSString *subString3 = [str substringFromIndex:4]; //@"5" NSString *subString4 = [str substringFromIndex:5]; //@"" // NSString *subString5 = [str substringFromIndex:6]; //会放生越界错误 // NSString *subString10 = [str substringToIndex:-1]; //会放生越界错误 NSString *subString11 = [str

extract digits from string in Obj-C [duplicate]

夙愿已清 提交于 2020-02-01 05:44:25
问题 This question already has answers here : iphone sdk - Remove all characters except for numbers 0-9 from a string [duplicate] (3 answers) Closed 6 years ago . I'm new to Objective-C, but experienced in other higher languages. I want to normalize a string by removing all non-numeric characters. In other words given the input string "206-555-1212" the normalized result should be "2065551212". The code snippet below works, but given my experience in other languages that seems like overkill. Is

面试基础题

社会主义新天地 提交于 2020-01-31 04:53:21
1. Object-c的类可以多重继承么?可以实现多个接口么?Category是什么?重写一个类的方式用继承好还是分类好?为什么? Object-c的类不可以多重继承;可以实现多个接口,通过实现多个接口可以完成C++的多重继承; Category是类别,一般情况用分类好,用Category去重写类的方法,仅对本Category有效,不会影响到其他类与原有类的关系。 2.#import 跟#include 又什么区别,@class呢, #import<> 跟 #import”"又什么区别? #import是Objective-C导入头文件的关键字, #include是C/C++导入头文件的关键字,使用#import头文件会自动只导入一次,不会重复导入,相当于#include和#pragma once; @class告诉编译器某个类的声明,当执行时,才去查看类的实现文件,可以解决头文件的相互包含; #import<>用来包含系统的头文件,#import””用来包含用户头文件。 3. 属性readwrite,readonly,assign,retain,copy,nonatomic 各是什么作用,在那种情况下用? readwrite 是可读可写特性;需要生成getter方法和setter方法时 readonly 是只读特性 只会生成getter方法 不会生成setter方法

AFNetworking二次封装的那些事

ぐ巨炮叔叔 提交于 2020-01-30 10:00:00
AFNetworking可是iOS网络开发的神器,大大简便了操作.不过网络可是重中之重,不能只会用AFNetworking.我觉得网络开发首先要懂基本的理论,例如tcp/ip,http协议,之后要了解web的请求和响应,会使用苹果自带的NSURLSession,最后是把AFNetworking的源码啃掉. 前言 一直以来网络开发用的都是前面同事基于AFNetworking二次封装好的框架,一直都没什么问题,也就没往深处去了解.然后公司开始新项目了,iOS端由我负责,这可是我的第一次啊,从零开始,构建整个项目.这是个挑战,内心还是有点小激动的. 轮子肯定是不用重复造的,网络框架就拿的老项目的,结果出现了两个问题. 上传多张图片,服务端解析不了 无文件上传, Content-Type还是multipart/form-data 为了解决这个问题,从就没用过的NSURLSession到http协议,追本溯源,终于解决了. http协议 关于http协议的理论就不多讲了,主要就讲使用POST方法传递数据时,发送的请求头和请求体. Content-Type 我们提交的数据是什么编码方式服务端是不知道的,其实我们完全可以自定义格式,只要服务端取到数据后能解析就可以了. 一般服务端语言如 php、python 等,以及它们的 framework,都内置了自动解析常见数据格式的功能

Pointers on Objective-c

这一生的挚爱 提交于 2020-01-29 14:43:46
问题 From what I understand (and please correct me if I'm wrong): int x, count = 10; int *hello; hello = &count; x = *hello; Here the variables x and count are declared to be of type integer. Additionally, the variable count is assigned the value of 10. hello is a pointer to type integer. hello is then assigned the address of count. In order to access the value of count, hello must have an asterisk in front of it, ie, *hello. So, x is assigned the value of whatever is in count and in this case, 10

Pointers on Objective-c

≡放荡痞女 提交于 2020-01-29 14:34:06
问题 From what I understand (and please correct me if I'm wrong): int x, count = 10; int *hello; hello = &count; x = *hello; Here the variables x and count are declared to be of type integer. Additionally, the variable count is assigned the value of 10. hello is a pointer to type integer. hello is then assigned the address of count. In order to access the value of count, hello must have an asterisk in front of it, ie, *hello. So, x is assigned the value of whatever is in count and in this case, 10

Using a String representing the name of a variable to set the variable

陌路散爱 提交于 2020-01-29 05:44:09
问题 This is a basic example that I know can be simplified, but for testing sake, I would like to do this in such a way. I want to set a variable based on an appending string (the variables "cam0" and "pos1" are already declared in the class). The appending string would essentially be an index, and i would iterate through a loop to assign cameras (cam0, cam1..) to different positions (pos0, pos1..). cam0 is defined as an UIImageView pos1 is defined as a CGRect This works for a NSString Variable