NSString的常用方法

创建一个新字符串并将其设置为 path 指定的文件的内容,使用字符编码enc,在error上返回错误

+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;

创建一个新字符串并将其设置为 url 指定的内容,使用字符编码enc,在error上返回错误
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;

创建一个新的空字符串

+(id)string

创建一个新的字符串,并将其内容设置为string的内容

+ (id)stringWithString:(NSString *)string;

将新分配的字符串设置为astring的内容

- (id)initWithString:(NSString *)aString;

将字符串设置为path指定的文件的内容 使用字符编码enc,在error上返回错误

- (id)initWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error;

将字符串设置为path指定的文件的内容 使用字符编码enc,在error上返回错误

- (id)initWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error;

返回字符串中的字符数目

- (NSUInteger)length;

返回索引(某个值,比如i)所在的Unicode字符     
- (unichar)characterAtIndex:(NSUInteger)index;

返回从(?)开始到结尾的子字符串

- (NSString *)substringFromIndex:(NSUInteger)from;

返回从字符串开始的位置到(?)的子字符串
- (NSString *)substringToIndex:(NSUInteger)to;

根据指定范围返回子字符串
- (NSString *)substringWithRange:(NSRange)range;

比较两个字符串 (忽略大小写)

- (NSComparisonResult)caseInsensitiveCompare:(NSString *)string;

比较两个字符串

- (NSComparisonResult)localizedCompare:(NSString *)string;

测试字符串是否以aString开始

- (BOOL)hasPrefix:(NSString *)aString;

测试字符串是否以aString结尾

- (BOOL)hasSuffix:(NSString *)aString;

测试两个字符串是否相等

- (BOOL)isEqualToString:(NSString *)aString;

返回字符串 串中的每个单词的首字母大写,其余字母小写

- (NSString *)capitalizedString;

返回转换为小写的字符串

- (NSString *)lowercaseString;

返回转换为大写的字符串

- (NSString *)uppercaseString;

返回转换为UTF8编码格式的字符串

- (__strong const char *)UTF8String;

返回转换double类型的字符串

- (double)doubleValue;

返回转换float类型的字符串

- (float)floatValue;

返回转换int类型的字符串

- (int)intValue;

返回转换为NSInteger类型的字符串

- (NSInteger)integerValue

分类:
iOS开发(2) 移动开发(2) NSString 字符串 截取字符串

版权声明:本文为博主原创文章,未经博主允许不得转载。

1.定义一个字符串a, 截取a 的某一个部分,复制给b, b必须是int型

NSString *a = @"1.2.30";

int  b= [[a substringWithRange:NSMakeRange(4,2)] intValue];

NSLog(@"a:%@  \n",a  );

NSLog(@"b:%d",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位  (substringToIndex: n)(第n 位不算再内)

- (void)viewDidLoad

{

NSString  *a = @"i like long dress";

NSString *b = [a substringToIndex:4];

NSLog(@"\n b: %@",b);

}

b: i li


3。字符串从第n 位开始截取,直到最后 (substringFromIndex:n)(包括第 n 位)

- (void)viewDidLoad

{

NSString  *a = @"i like long dress";

NSString *b = [a substringFromIndex:4];

NSLog(@"\n b: %@",b);

}

b: ke long dress

。NSMutableString  为可变的字符串

NSString      为不可变的字符串

-(void)viewDidLoad

{

NSMutableString *a = [[NSMutableString  alloc]initWithString:@"123456798"];

NSLog(@" \n a:  %@\n",a);

[a deleteCharactersInRange:NSMakeRange(1,2)];

NSLog(@" \n a:  %@\n",a);

[a release];

}

4.截取字符串

NSString*string =@"sdfsfsfsAdfsdf";
string = [string substringToIndex:7];//截取掉下标7之后的字符串
NSLog(@"截取的值为:%@",string);
[string substringFromIndex:2];//截取掉下标2之前的字符串
NSLog(@"截取的值为:%@",string);

5.匹配字符串
NSString*string =@"sdfsfsfsAdfsdf";
NSRangerange = [stringrangeOfString:@"f"];//匹配得到的下标
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围类的字符串
NSLog(@"截取的值为:%@",string);

6.分隔字符串
NSString*string =@"sdfsfsfsAdfsdf";

NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf

NSString 的常见方法的更多相关文章

  1. NSString的常见方法

    //1.创建常量字符串. NSString *astring = @"This is a String!";   //2.创建空字符串,给予赋值. NSString *astrin ...

  2. iOS-NSString常见方法

    </pre><pre name="code" class="html">#import <Foundation/Foundatio ...

  3. UIPickerView常见属性、常见方法(包括代理方法和数据源方法)的一些说明

    一.UIPickerView 1.UIPickerView的常见属性 // 数据源(用来告诉UIPickerView有多少列多少行) @property(nonatomic,assign) id< ...

  4. C#图片处理常见方法性能比较

    C#图片处理常见方法性能比较 来自:http://www.cnblogs.com/sndnnlfhvk/archive/2012/02/27/2370643.html   在.NET编程中,由于GDI ...

  5. window对象中的常见方法

    <body><!-- window对象中的常见方法--><script type="text/javascript"> var timeid; ...

  6. python socket 常见方法及 简单服务/客户端

    socket 常见方法: 补充说明:what is file descriptor? 文件描述符是什么? 参考(http://stackoverflow.com/questions/8191905/w ...

  7. VBS操作Excel常见方法

    VBS操作Excel常见方法 作者: 字体:[增加 减小] 类型:转载 时间:2009-11-13我要评论 VBS控制Excel常见方法,需要的朋友可以参考下. dim oExcel,oWb,oShe ...

  8. jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...

  9. AJAX跨域的常见方法

    由于在工作中需要使用AJAX请求其他域名下的请求,但是会出现拒绝访问的情况,这是因为基于安全的考虑,AJAX只能访问本地的资源,而不能跨域访问.比如说你的网站域名是aaa.com,想要通过AJAX请求 ...

随机推荐

  1. linux中ctrl+z、ctrl+d和ctrl+c的区别

    ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样.ctrl+c是强制中断程序的执行,而ctrl+z的是将任务中断,但是此任务并没有结束,他仍然在进程中他只是维持挂起的状态,用户可以使用f ...

  2. mininet 中图形化界面的安装

    just run a GUI in VM console window First, log in to the VM in its console window (i.e. type directl ...

  3. ros消息时间同步与回调

    转载自http://blog.csdn.net/zyh821351004/article/details/47758433 方式一: 全局变量形式  : TimeSynchronizer 步骤: 1. ...

  4. Android-Tab单选控件

    今天看到项目中有一个控件写得很美丽,据说是github上开源的控件,地址没找到,例如以下图所看到的,很常见的效果,几个tab页面来回切换: 转载请标明出处:http://blog.csdn.net/g ...

  5. .net通用权限框架C/S概览

    通用权限框架cs部分 先概述一下,cs使用vs2010+sql2008 和bs公用同一个数据库 为使界面好看使用了第三方控件 donetbar和devexpress,正版是要收费的,但是你们都明白的可 ...

  6. c# 根据中文汉字获取到拼音

    public static String ConvertToPinyin(String str) { if (String.IsNullOrEmpty(str)) return String.Empt ...

  7. C# - 重定义一个接口的实现

    using System;using System.Collections.Generic;using System.Text; namespace MyTester{    public inter ...

  8. TCP/IP详解之:ICMP协议

    ICMP协议: ICMP是IP层的一个组成部分,ICMP报文是在IP数据报内部被传输的,用于在IP主机.路由器之间传递控制消息.控制消息是指网络不通.主机是否可达.路由是否可用等网络本身的消息.这些控 ...

  9. puts fputs printf的区别

    puts()显示字符串时自动在其后添加一个换行符,函数里的参数是一个地址,从该地址向后面输出,直到遇到空字符,所以要确保输出的字符串里要有空字符.与gets()函数一起使用. fputs()需要第二个 ...

  10. leetcode Same Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...