NSString
-----判断某个字符或者字符串个数------
http://stackoverflow.com/questions/938095/nsstring-number-of-occurrences-of-a-character
NSUInteger *Count;
Count=(NSUInteger *) [[yourString componentsSeparatedByString:@" "] count];
componentsSeparatedByString:
Returns an array containing substrings from the receiver that have been divided by a given separator.
Parameters
-
separator
-
The separator string.
Return Value
An NSArray
object containing substrings from the receiver that have been divided by separator.
Discussion
The substrings in the array appear in the order they did in the receiver. Adjacent occurrences of the separator string produce empty strings in the result. Similarly, if the string begins or ends with the separator, the first or last substring, respectively, is empty. For example, this code fragment:
NSString *list = @"Norman, Stanley, Fletcher"; |
NSArray *listItems = [list componentsSeparatedByString:@", "]; |
produces an array { @"Norman", @"Stanley", @"Fletcher" }
.
If list begins with a comma and space—for example, ", Norman, Stanley, Fletcher"
—the array has these contents: { @"", @"Norman", @"Stanley", @"Fletcher" }
If list has no separators—for example, "Norman"
—the array contains the string itself, in this case { @"Norman" }
.
NSString * str = [[ NSString alloc ] init ];
NSString * str = [[[ NSString alloc ] init ] autorelease ];
注意:在NSString 中存在自己的实例化和初始化的方法 例如:
NSString * str1 = [ NSString stringWithCString :"new String" enconding : NSACIIStringEncoding ];
NSString * str2 = [ NSString alloc ] stringWithCString :"new String" enconding : NSACIIStringEncoding];
str1和str2两个对象是相同的。
--NSStringEncoding 中常用的字符编码 ----------------
NSASCIIStringEncoding
NSUTF8StringEncoding
NSUnicodeStringEncoding
--NSString创建实例----------------
带“@”符号的方法只能定义含有英文和数字的NSString实例,例如:
NSString * str = "Hello money~";
--生成含有中文的NSString方法-------------
//此方法自动释放内存
+ (id) stringWithCString :(const char*)cString encoding:(NSStringEncoding)encoding;
//进行alloc后进行初始化
- (id)initWithCString:(const char*)cString encoding:(NSStringEncoding)encoding;
例如:
NSString *string = [ NSString stringWithCString :"您好" encoding: NSUTF8StringEncoding ];
NSString *string = [[ NSString alloc ] initWithCString:"您好" encoding: NSUTF8StringEncoding ];
--使用格式创建字符串-------------
+ (id)stringWithFormat:( NSString *)format...
- (id)initWithFormat:( NSString *)format...
例如:
NSString * str = "hello";
NSString *string = [ NSString stringWithFormat:@"%@ world", str ];
NSLog(string); 结果:hello world
--常用的替换符--------------
%@ NSString实例
%d,%D,%i 整数
%u,%U 无符号整数
%x 将无符号整数以十六进制小写字母显示
%X 将无符号整数以十六进制大写字母显示
%f 小数
%c 字符
%s C语言字符串
%% 显示%字符本身
--------------------------
NSRange
--NSRange的定义
typedef struct _NSRange
{
unsigned int location;
unsigned int length;
}NSRange;
NSMakeRange函数
--这个函数比较特殊 返回一个NSRange的对象。
NSMakeRanger(unsigned int location,unsigned int length);
例如:
NSRange range = NSMakeRanger(0,5);
NSLog(@"location is %d,length is %d", range.location , range.length );
---------------------------
计算字符串长度
- (unsigned int)length;
---------------------------
字符串连接,插入和删除
1、连接
- ( NSString *)stringByAppendingString:( NSString *)string;
- ( NSString *)stringByAppendingFormat:( NSString *)format...;
例如:
NSString * str1 = @"hello";
NSString * str2 = @"world";
NSString *str3 = [ str1 stringByAppendingString: str2 ];
NSString *str4 = [ str2 stringByAppendingFormat:@"%d...%d",10,20];
str4 --> world 10...20
-----------------
NSMutableString的生成
NSString + (id)string; //生成空字符串的实例
+ (id)stringWithString:( NSString *)string; //带自动释放内存
- (id)initWithString:( NSString *)string;
例如:
NSMutableString *string = [NSMutableString stringWithString:@"hello"];
2、追加字符串
NSMutableString
+ (void)appendString:( NSString *)string;
- (void)appendFormat:( NSString *)format...;
例如:
NSMutableString string = [NSMutableString string];
[string appendString:@"hello"];
[string appendString:@"money"];
[string appendString:@" and world"];
3、插入字符串
NSMutableString
+ (void)insertString:( NSString *)string atIndex:(unsigned)index;
从index位置插入字符串
例如:
NSMutableString *string = [NSMutableString stringWithString:@"Mac X"];
[string insertString:@"OS" atIndex:4];
string --> Mac OS X
4、删除字符串
NSMutableString
+ (void)deleteCharactersInRange:(NSRange)range;
例如:
NSMutableString *string = [NSMutableString stringWithString:@"Mac os"];
[string deleteCharactersInRange:NSMakeRanger(0,1)];
NSLog(string);
string -->ac os;
5、字符串比较
NSString
- (BOOL)isEqualToString:( NSString *)string;
6、比较前置串和后置串
NSString
- (BOOL)hasPrefix:( NSString *)string;
- (BOOL)hasSuffix:( NSString *)string;
例如:
NSString * str1 = @"Mac OS";
NSString * str2 = @"Mac Pro";
BOOL flag;
flag = [ str1 hasPrefix:@"Mac"]; YES
flag = [ str2 hasSuffix:@"OS"]; NO
7、字符串检索
NSString
//如果找到就返回范围,否则NSRange的location项被设置为NSNotFound
- (NSRange)rangeOfString:( NSString *)subString;
- (NSRange)rangeOfString:( NSString *)subString option:(unsigned)mask;
- (NSRange)rangeOfString:( NSString *)subString option:(unsigned)mask range:(NSRange)range;
-----mask常用选项列表
NSCaseInsensitiveSearch 不区分字母大小写
NSLiteralSearch 对字符串进行字节单位的比较,一般可提高检索速度
NSBackwardsSearch 从范围的末尾开始检索
NSAnchoredSearch 仅检索制定范围的前部。忽略字符串中间的检索字符
例如:
NSString *string = @"hello world";
NSRange range = [string rangeOfString:@"he"];
if( range.location != NSNotFound)
{
NSLog(@" location=%d,length=%d", range.location , range.length );
}
8、截取字符串
NSString
- ( NSString *)substringToIndex:(unsigned)index; //返回字符串开头至index位的字符串 不包含索引位
- ( NSString *)substringFromIndex:(unsigned)index; //返回字符串第index位至末尾的字符串 包含索引位
- ( NSString *)substringWithRange:(NSRange)range; //返回字符串中范围range内的字符串 包含索引位
例如:
NSString *string = [string substringWithRange:NSMakeRange(5,2)];
9、读取文本文件
NSString
+ (id)stringWithContentsOfFile:( NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error //自动释放内存
- (id)initWithContentsOfFile:( NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
例如:
NSString *string = [ NSString stringWithContentsOfFile:@"/user/test/ yw.txt " encoding: NSUTF8StringEncoding error:&error];
if(string){}
10、输出文本文件
NSString
- (BOOL)writeToFile:( NSString *)path atomically:(BOOL)useAuxiliaryFile encoding:(NSStringEncoding)enc error:(NSError **)error
//参数 atomically 暂时将文件保存到辅助文件中
//path
The file to which to write the receiver. If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath before invoking this method.
----下面是网上找的例子 感谢 @chenshizero
//扩展路径
NSString *Path = @"~/ NSData.txt ";
NSString *absolutePath = [Path stringByExpandingTildeInPath];
NSLog(@"absolutePath:%@",absolutePath);
NSLog(@"Path:%@",[absolutePath stringByAbbreviatingWithTildeInPath]);
//文件扩展名
NSString *Path = @"~/ NSData.txt ";
NSLog(@"Extension:%@",[Path pathExtension]);