Objective-C中的hasPrefix
2016-05-06 21:08
405 查看
Objective-C中的hasPrefix
Objective-C中的hasPrefixhasPrefix:方法的功能是判断创建的字符串内容是否以某个字符开始,其语法形式如下:
-(BOOL)hasPrefix:(NSString*)aString;
其中,(NSString *)aString;用来指定字符串。该方法的返回值为BOOL,当BOOL为YES或者为1时,则字符串是以某个字符开始;当BOOL为NO或者为0时,则字符串不是以某个字符开始。 【示例】以下程序通过使用hasPrefix:方法来判断使用stringWithCString:方法创建的字符串是否以字母B开始,如果是以字母B开始,输出开头为字母B;如果不是,输出开头不为字母B。程序代码如下:
#import <Foundation/Foundation.h> int main(int argc,constchar* argv[]) { @autoreleasepool{ NSString*a=[NSString stringWithCString:"ABCDEF" encoding:NSASCIIStringEncoding]; if([a hasPrefix:@"B"]==YES){//判断字符串是否以B字符开始 NSLog(@"开头为字母B"); }else{ NSLog(@"开头不为字母B"); } } return0; }
运行结果如下:
2013-03-1918:10:02.0874-13[751:303]开头不为字母B
相关文章推荐
- Object Detection with DPM
- Object-c------Foundation中的结构体和字符串
- 使用PHPExcel 时报错ZipArchive::getFromName(): Invalid or uninitialized Zip object
- iOS之'mutating method sent to immutable object'
- error LNK2019: 无法解析的外部符号 __imp_DeleteObject
- Programming with Objective-C(五)
- 关于List<Map<String,Object>>的通俗解释
- JS中六种数据类型(六)——Object
- 将json格式的字符串转化成object对象
- Object-c------protocol
- Object-c------block
- 解决ImportError: libmysqlclient_r.so.16: cannot open shared object file
- objective-c - 基础篇 - UIWebView
- Hibernate:deleted object would be re-saved by cascade (remove deleted object from associations): []
- Object-c字符串操作
- 【Json】关于json解析时异常org.json.JSONException: A JSONObject text must begin with '{' at character 1 of {的解决方法
- Orocos DataPort 解析: orocos lock free data object
- inside the c++ object
- Objective-C的属性和成员变量用法及关系浅析
- iPhone开发:Objective C 代码规范-iOS总结版