您的位置:首页 > 其它

OC中的字符串(NSString)、字符串数组(NSArray)和字典(NSDictionary)

2015-12-05 16:44 351 查看
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><1>NSString字符串比较方法compare,返回值NSComparisionResult有3个枚举值:NSOrderedAscending(升序),NSOrderedSame(同样),NSOrderedDiscending(降序)。</span>
判断字符串内容是否相等的函数:isEqualToString。

前后缀检查函数:hasPrefix,hasSuffix。

字符串查找函数:rangeOfString,返回值类型NSRang(NSRange是OC中的常见类型,本质是一个结构体,包括location,length两个成员变量)。

NSRange创建的函数:NSMakeRange(location,length);

NSRange转换成NSString:NSStringFromRange。

NSString的截取和替换:subStringFromIndex:NSUInteger index,从index开始到字符串结尾;

subStringToIndex:NSUInteger index,从字符串开头到index处;

subStringWithRange:NSRange range;获取字符串中range内的子字符串;

字符串的替换:StringByReplacingOccurencesOfString: target withStringreplacement;

NSString转换成基本数据类型:intValue,floatValue,doubleValue;

NSString类型和String类型的转换:NSString stringWithUTF8String; UTF8String;

<2>NSMutableString概念和使用 NSMutableString类型相当于字符串链表,空间和内容是可变的,其常见用法如下:
(1)增:appendString,insertString;
(2)删:deleteCharactersInRange:NSRange ns;
(3)改:replaceCharactersInRange;
(4)查:containsString;
(5)通过string属性能覆盖掉原来的字符串内容;

<3>NSArray的介绍和使用,NSArray的特点如下:
(1)NSArray中存储的是任意OC的对象,且其中元素是有序的,不能存放非OC对象,其对象被定义后内容是不可变的,既不能添加
   元素,也不能删除元素;
(2)创建方式:array方法创建新的空数组;arrayWithObjects,alloc initWithObects;NSArray中不能存放nil对象,nil是数组
   结束的标志;
(3)基本方法:count;objectAtIndex;containsObject;indexOfObject;
(4) 简化定义方式:@[@“”,””];
(5) NSArray的遍历方法:通过下标;快速枚举法(for);block(enumerateObjectsUsingBlock);
(6) NSArray读写文件的方法:writeToFile,arrayWithContentsOfFile;
(7) NSArray和字符串的相互转换:componentsJoinedByString,componentsSeparatedByString;

<4>NSMutableArray及基本使用
(1)定义:arrayWithCapacity:5;
(2) 增:addObject,insertObject;删:removeObject,removeObjectAtIndex,removeAllObjects;改:remplaceObjectAtIndex:
   withObject;查:containsObject;
(3) 交换元素:exchangeObjectAtIndex: withObjectAtIndex:

<5>NSDictionary的介绍及使用 
(1)定义:dictionaryWithObject: forKey: dictionaryWithObjectsAndKeys:
(2)字典的key值和value值都必须是对象;
(3)快速创建字典:@{key:value...};
(4)key值不能重复;
(5)基本方法:count,objectForKey:遍历:enumerateKeysAndObjectUsingBlcok;
(6)文件读写函数:writeToFile,dictionaryWithContentsOfFile;

<6>NSMutableDictionary介绍和使用 
增:setValue: forKey: 删:removeObjectsForKey,removeAllObjects;改:setObject: forKey:,查:containsObject;

<7>使用NSURL读写字符串
(1)URL:Uniform Resource Locator:统一资源定位符;定义的一般格式:协议头://主机域名//路径;
(2)Http://超文本传输协议 https://前缀表明是用SSL加密的,信息传输会更加安全; (3)NSURL的定义:URLWithString;fileURLWithPath;

// NSString前后缀及查找
//
// Created by MAC on 15/11/28.
// Copyright © 2015年 zhudong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *s1 = @"https:\\www.baidu.com";

if ([s1 hasPrefix:@"http:"]||[s1 hasPrefix:@"https:"]) {
NSLog(@"是一个网址");
}

NSString *s2 = @"xxx.jpg";
if ([s2 hasSuffix:@".jpg"]) {
NSLog(@"是一个图片");
}
NSString *s3 = @"dadfsdfdmeidfasfsdf";
NSString *s4 = @"mei";
NSRange ns = [s3 rangeOfString:s4];
if (ns.location != NSNotFound) {
NSLog(@"%@在%@内的位置是:%lu,其长度是:%lu",s4,s3,ns.location,ns.length);
}
NSString *s5 = @"abc";
NSString *s6 = [NSString stringWithFormat:@"abc"];
if ([s5 isEqualToString:s6]) {
NSLog(@"s5和s6相等");
}

}
return 0;
}
// 字符串的截取和替换
//
// Created by MAC on 15/11/28.
// Copyright © 2015年 zhudong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
// substringfrom
NSString *str = @"http:\\www.baidu.com";
NSString *result = [str substringFromIndex:5];
NSLog(@"%@",result);
// substringto
result = [str substringToIndex:5];
NSLog(@"%@",result);

// substringwithrang
//NSRange ns={5,2};
result = [str substringWithRange:(NSRange){5,2}];
NSLog(@"%@",result);
//字符串的截取
NSUInteger loc = [str rangeOfString:@"w"].location +1 ;
NSUInteger len = [str rangeOfString:@"c"].location - [str rangeOfString:@"w"].location;
NSRange ns = NSMakeRange(loc, len);
result = [str substringWithRange:ns];
NSLog(@"%@",result);

// stringreplacingoccurencesofstring
result = [str stringByReplacingOccurrencesOfString:@"w" withString:@"*"];
NSLog(@"%@",result);
}
return 0;
}
// NSArray遍历的三种方法
//
// Created by MAC on 15/12/1.
// Copyright © 2015年 zhudong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"zhu";
NSArray *arr = @[@"one",@"two",[NSString stringWithFormat:@"sting%@",str]];
//1 for循环
for (int i = 0; i < arr.count; i++) {
NSLog(@"%@",arr[i]);
}
//2 for in 增强循环
for (id str in arr) {
NSLog(@"%@",str);
}
//3 block 方法
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"idx = %ld,obj = %@",idx,obj);
}];
}
return 0;
}
// NSArray文件的读写
//
// Created by MAC on 15/12/1.
// Copyright © 2015年 zhudong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *arr = @[@"one",@"Two",@"Three"];
BOOL isWrite = [arr writeToFile:@"arr.plist" atomically:YES];
if(isWrite) {
NSLog(@"写入成功!");
}
NSArray *arr1 = [NSArray arrayWithContentsOfFile:@"arr.plist"];
[arr1 enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSLog(@"idx = %ld
4000
,obj = %@",idx,obj);
}];
}
return 0;
}
// NSDictionary的使用
//
// Created by MAC on 15/12/2.
// Copyright © 2015年 zhudong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
//1 空字典
NSDictionary *dict1 = [NSDictionary dictionary];
NSLog(@"%@",dict1);
//2 一个键值对的字典
NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"zhu" forKey:@"1"];
NSLog(@"%@",dict2);
//3 多个键值对的字典
NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:@[@"zhang",@"wang",@"li",@"zhao"]forKeys:@[@"1",@"2",@"3",@"4"]];
NSLog(@"%@",dict3);
//通过键获取值
NSString *str = [dict3 valueForKey:@"1"];
NSLog(@"%@",str);
//字典的遍历
for (NSString *str in dict3) {
NSLog(@"%@",[dict3 valueForKey:str]);
}
//用块遍历字典
[dict3 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key = %@,obj = %@",key,obj);
}];

}
return 0;
}// NSMutableDictionary的使用
//
// Created by MAC on 15/12/2.
// Copyright © 2015年 zhudong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *hn = @[@"zhengzhou",@"zhoukou"];
NSArray *fj = @[@"xiamen",@"fuzhou"];
NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:hn,@"henan",fj,@"fujian", nil];
[dict1 enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key = %@,obj = %@",key,obj);
}];
for (NSArray *arr in [dict1 allKeys]) {
for (NSString *str in [dict1 objectForKey:arr]) {
NSLog(@"%@",str);
};
};
NSMutableDictionary *ns = [NSMutableDictionary dictionaryWithCapacity:3];
[ns setObject:@"zhu" forKey:@"one"];
NSLog(@"%@",ns);
}
return 0;
}// 使用NSURL读写文件
//
// Created by MAC on 15/12/5.
// Copyright © 2015年 zhudong. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str3 = @"gangjiong";
//URL创建的两种方式
//(1)URLWithString
NSURL *url = [NSURL URLWithString:@"file:///users/mac/desktop/URL.txt"];
// (2) fileURLWithPath
NSURL *url1 = [NSURL fileURLWithPath:@"/users/mac/desktop/url1.txt"];
NSError *err;
[str3 writeToURL:url1 atomically:YES encoding:NSUTF8StringEncoding error:&err];
if (err == nil) {
NSLog(@"url写入成功!");
}else{
NSLog(@"url写入失败!");
}
NSString *str4 = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err];
if (err == nil) {
NSLog(@"str4 = %@",str4);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: