iOS字符串处理
拼接字符串
NSString* string;
NSString* string1, string2;
//方法1.
string = [NSString initWithFormat:@"%@,%@", string1, string2 ];
//方法2.(常用)
string = [string1 stringByAppendingString:string2];
//方法3 .
string = [string stringByAppendingFormat:@"%@,%@",string1, string2];
清除字符串 首尾空格及换行符
//清除首尾空格
//定义宏
#define allTrim(object) [object stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]
//调用宏
NSString *emptyString = @" ";
if ([allTrim( emptyString ) length] == 0 ) {
NSLog(@"Is empty!");
} //清除所有空格(替换操作)
NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@" " withString:@""]; //清除换行字符
headerData = [headerData stringByReplacingOccurrencesOfString:@"\r" withString:@""];
headerData = [headerData stringByReplacingOccurrencesOfString:@"\n" withString:@""];
判断空值处理 (null)与 <null>
//见过最好的解决方法之一, 改进的内联函数,源自Git Hub
// Check if the "thing" pass'd is empty static inline BOOL isEmpty(id thing) {
return thing == nil
|| [thing isKindOfClass:[NSNull class]]
|| ([thing respondsToSelector:@selector(length)]
&& [(NSData *)thing length] == 0)
|| ([thing respondsToSelector:@selector(count)]
&& [(NSArray *)thing count] == 0);
}
//判断UITextField.text是否为空
用户未进行任何输入的情况下为nil. (yourTextField.text 在最初创建后的值为nil,nil不等于@"")
[yourTextField.text isEqualToString:@""] || yourTextField.text == nil
//或者
yourTextField.text.length == 0
//其他方法一
//判断字符串是否为空
#define strIsEmpty(str) ([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1 ? YES : NO )
//方法二
- (BOOL) isBlankString:(NSString *)string { if (string == nil || string == NULL) {
return YES;
}
if ([string isKindOfClass:[NSNull class]]) {
return YES;
}
if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
return YES;
}
return NO;
}
通过枚举值调对应字符串
//定义枚举类型
typedef NS_ENUM(NSInteger, SubCityCategoryType){
SomeTypeKey = 0,
};
//声明全局类型数组
extern SomeType const SomeTypes[];
//声明全局类型字符串函数
extern NSString * const SomeTypeIdentifier(SomeType Key);
//定义类型数组
SubCityCategoryType const SubCityCategoryTypes[] = {
SomeTypeKey
};
//定义字符串函数
NSString * const SubCityCategoryTypeIdentifier(SomeType Key){
switch (Key) {
case SomeTypeKey:
return @"KeyString";
default:
return @"";
}
}
//传入枚举值通过字符串函数获取指定字符串
SubCityCategoryTypeIdentifier(SomeTypeKey) //使用方式
使用JSONKit将字符串,字典,数组转换成json格式
str = [string JSONString];
str = [dic JSONString];
str = [array JSONString];
iOS字符串处理的更多相关文章
- iOS字符串加密至MD5&及获取文件MD5
iOS 字符串加密至MD5 #import <CommonCrypto/CommonDigest.h> + (NSString *) md5:(NSString *)str { const ...
- iOS 字符串 MD5
iOS 字符串 MD5 Objective-C 实现 需要引入头文件 #import <CommonCrypto/CommonCrypto.h> 这里用方法实现 + (nullable N ...
- iOS字符串安全
iOS字符串安全 一个编译成功的可执行程序,其中已初始化的字符串都是完整可见的. 针对于iOS的Mach-O二进制通常可获得以下几种字符串信息: 资源文件名 可见的函数符号名 SQL语句 format ...
- iOS 字符串处理笔记
iOS字符串处理笔记,包括如何使用正则表达式解析,NSScanner扫描,设置和使用CoreParse解析器来解析处理自定义符号等内容 搜索 在一个字符串中搜索子字符串 最灵活的方法 - (NSRan ...
- ios字符串截取
最近刚从 . net 转到ios平台 又开始了新的学习,所以开始写博客,这样可以让我每天都能进步一点点 对字符串的操作很多情况下和c#中的不一样 1.字符串的声明 //声明字符串 NSString ...
- iOS 字符串的宽度和高度自适应
//获取字符串的宽度 -(float)widthForString:(NSString *)value fontSize:(float)fontSize andHeight:(float)height ...
- iOS 字符串删除 DOM
iOS string 删除 包含的 DOM NSMutableString *mutableString = [NSMutableString stringWithString:responseSt ...
- iOS字符串为空的判断
//判断字符串 -(BOOL) isValidString:(id)input { if (!input) { return NO; } if ((NSNull *)input == [NSNull ...
- ios字符串操作
string的操作应用 NSRange range = [self.general rangeOfString:@"."]; NSString *str = [self.gener ...
- ios字符串计算高度总结
1.用xib的话,设置约束的时候 不设置lable的高度即可,高度返回的就是最优高度. 2.用lable代码计算高度 CGFloat getHeightForLableString(NSString ...
随机推荐
- Extjs4 中date时间格式的问题
在Grid中显示时间,后台传过来的是date格式的数据(PHP date('Y-m-d', time()),一般在Ext model中定义数据的类型和格式: {name:'birth', type:' ...
- Linux经常使用命令(十八) - find概述
Linux下find命令在文件夹结构中搜索文件,并运行指定的操作.Linux下find命令提供了相当多的查找条件,功能非常强大.由于find具有强大的功能,所以它的选项也非常多.当中大部分选项都值得我 ...
- Java基础数据类型的默认值
1.整数类型(byte.short.int.long)的基本类型变量的默认值为0. 2.单精度浮点型(float)的基本类型变量的默认值为0.0f. 3.双精度浮点型(double)的基本类型变量的默 ...
- CentOS firewalld 防火墙操作
Centos 7 开启端口CentOS 7 默认没有使用iptables,所以通过编辑iptables的配置文件来开启80端口是不可以的 CentOS 7 采用了 firewalld 防火墙 如要查询 ...
- linux下安装oracle sqlplus以及imp、exp工具
一.下载oracle 11g sqlplus软件 linux 64位操作系统,oracle安装包地址 http://www.oracle.com/technetwork/topics/linuxx86 ...
- 穿透Session 0 隔离(一)
服务(Service)对于大家来说一定不会陌生,它是Windows 操作系统重要的组成部分.我们可以把服务想像成一种特殊的应用程序,它随系统的“开启-关闭”而“开始-停止”其工作内容,在这期间无需任何 ...
- 巨蟒python全栈开发flask4
1.偏函数 2.ThreadingLocal线程安全 空间换取时间 3.LocalStack 4.RunFlask+request 5.请求上文 6.请求下文
- The Log: What every software engineer should know about real-time data's unifying abstraction
http://engineering.linkedin.com/distributed-systems/log-what-every-software-engineer-should-know-abo ...
- In-Stream Big Data Processing
http://highlyscalable.wordpress.com/2013/08/20/in-stream-big-data-processing/ Overview In recent y ...
- 利用epoll实现异步IO
之前异步IO一直没搞明白,大致的理解就是在一个大的循环中,有两部分:第一部分是监听事件:第二部分是处理事件(通过添加回调函数的方式).就拿网络通信来说,可以先通过调用 select 模块中的 sele ...