Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重新给这个字符串赋值。而NSMutableString 创建赋值以后可以动态在该字符串上更改内容与长度。
// 创建字符串
NSString *str = @"jack";
NSString *s1 = [[NSString alloc] initWithFormat:@"age = %d" ,]; // c字符串转oc字符串
NSString *s2 = [[NSString alloc] initWithUTF8String:"jack"]; // oc 转 c
const char *c1 = [s2 UTF8String];
NSLog(@"%s", c1);
字符串的遍历
NSString *str = @"ni hao peng you";
for (int i = ; i < str.length; i++) {
unichar c = [str characterAtIndex:i];
NSLog(@"%c",c);
}
NSURL
// NSUTF8StringEncoding 用到中文可以用这个
// 传进来文件路径可以查看文件
NSString *s3 = [[NSString alloc] initWithContentsOfFile:"/Users/apple/Desktop/1.txt" encoding:NSUTF8StringEncoding error:nil];
/*URL :资源路径
协议头://路径
file://
ftp://
http://www.baidu.com
*/
// 在 iOS 程序访问 HTTP 资源时需要对 URL 进行 Encode
// NSURL *url = [[NSURL alloc] initWithString:@"file:///Users/apple/Desktop/1.txt"]; NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/1.txt"]; NSString *s4 = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"s4=\n%@", s4);
NSString :不可变字符串
NSMutableString :可变字符串
字符串中拼接
// 创建字符串
NSMutableString *s = [NSMutableString stringWithFormat:@"my age is 23"];
// 拼接内容到s的后面
[s appendString:@" 3 21"];
字符串中删除元素
//创建字符串
NSMutableString *str = [NSMutableString stringWithString:@"haha 你好!!"];
//删除字符串中含"haha"的字符
[str deleteCharactersInRange: [str rangeOfString: @"haha"]];
NSLog(@"str = %@",str);
字符串查找
查找一个字符串在另一个字符串首次出现的位置
NSString *s = @"abciOSdefg";
NSString *s1 = @"iOS";
NSRange * range = [s rangeOfString:s1];
// 判断是否找到字符串
if(range.location!NSNotFound){
NSLog(@"%lu,%lu",range,location, range.length);
}else{
NSLog(@"没有找到字符串位置");
}
判断字符串是否相同
// 字符串是否相同
NSString *s1 = @"abc"; NSString *s2 = [NSString stringWithFormat:@"abc"]; if ([s1 isEqualToString:s2]) {
NSLog(@"相同");
}else{
NSLog(@"不相同");
}
字符串截取
NSString *str = @"";
// 从元素下标为3的位置开始往后截取,(包含下标为3的位置的元素)
NSString *str1 = [str substringFromIndex:];
NSLog(@"str1 = %@",str1);
// 从开始位置截取到3位(不包含下标为3的位置的元素)
NSString *str2 = [str substringToIndex:];
NSLog(@"str2 = %@",str2);
// 截取某段位置的字符进行输出
NSRange range ={,};
NSString *str3 = [str substringWithRange:range];
NSLog(@"str3 = %@",str3);
// 截取某段位置的字符
// 012345 789
NSString *str = @"aaaaa<你好>aaaaaa";
NSUInteger loc = [str rangeOfString:@"<"].location + ; // 传的位置
NSUInteger len = [str rangeOfString:@">"].location - loc; // 要截取的字符长度 NSRange r1 = {loc, len}; NSString *subStr = [str substringWithRange:r1];
NSLog(@"subStr = %@", subStr);
字符串的替换
// 字符串的替换
NSString *str = @"adadwffdtoiajdfa"; // str stringByReplacingOccurrencesOfString:@"源字符串" withString:@"要替换成的新内容"
// 把a替换成/
NSString * newStr = [str stringByReplacingOccurrencesOfString:@"a" withString:@"/"];
NSLog(@"newStr = %@", str); // 把字符串中的空格去掉
str = @" adad wff dt oiaj df a";
newStr = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"newStr = %@", newStr); // 把字符串中空格去掉,把星号替换成/
str = @"http ** www* baidu* com*"; newStr = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *newStr1 = [newStr stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
NSLog(@"newStr1 = %@",newStr1);
Objective-C( Foundation框架 一 字符串)的更多相关文章
- Objective - c Foundation 框架详解2
Objective - c Foundation 框架详解2 Collection Agency Cocoa provides a number of collection classes such ...
- Objective-C语言Foundation框架
Mac OS X开发会使用Cocoa框架,它是一种支持应用程序提供丰富用户体验的框架,它实际上由:Foundation和Application Kit(AppKit)框架组成.iOS开发,会使用Coc ...
- OC 知识:Foundation 框架及相关类详尽总结
本文用来介绍Foundation框架的相关知识,以及Foundation框架所提供类的相关知识总结. 1. 框架介绍 框架是由很多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使开发程序变得更加 ...
- Foundation框架—字符串
Foundation框架—字符串 一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:O ...
- 李洪强iOS之Foundation框架—字符串
Foundation框架—字符串 一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:O ...
- OC Foundation框架—字符串
一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...
- Objective-C( Foundation框架 一 常见的结构体)
常见的结构体 (NSPoint,CGPoint).(NSRange,CGRange).(NSSize,CGSize) 苹果官方推荐使用CG开头的结构体 NSRange是Foundation框架中常见的 ...
- iOS开发系列—Objective-C之Foundation框架
概述 我们前面的章节中就一直新建Cocoa Class,那么Cocoa到底是什么,它和我们前面以及后面要讲的内容到底有什么关系呢?Objective-C开发中经常用到NSObject,那么这个对象到底 ...
- Foundation框架-NSString和NSMutableString
可变与不可变的字符串 --1-- Foundation框架介绍 1.1 框架介绍 --2-- NSString 2.1 NSString介绍及使用 2.2 NSString创建方式 2.3 从文件中 ...
随机推荐
- Java:并行编程及同步使用方法
知道java可以使用java.util.concurrent包下的 CountDownLatch ExecutorService Future Callable 实现并行编程,并在并行线程同步时,用起 ...
- windows系统调用 遍历进程的虚拟地址
#include "iostream" #include "windows.h" #include "shlwapi.h" #include ...
- WIN7远程桌面连接方法!
WIN7远程桌面连接方法!
- 如何清除PL/SQL中的缓存
每次查询前清空缓存10g以上:alter system flush buffer_cache;9i:ALTER SESSION SET EVENTS 'immediate trace name flu ...
- Qt之重写QLabel类
在mylabel.h 文件中#ifndef MYLABEL_H#define MYLABEL_H #include <QLabel>/*重新实现QLabel类,使其支持点击事件*/clas ...
- WDR7500 花生壳问题
新进一WDR7500 居然不能解析花生壳. 百度一番, 发现别人有同样的问题. 找来找去从别人的只言片语中发现需要升级固件. 好不容易加入一个群, 把固件下下来. 升级固件, 重新设置, 解析成功. ...
- 删除已经配置的类库和移除CocoaPods[转]
转自:http://blog.csdn.net/jymn_chen/article/details/19213601 引言 在使用CocoaPods(一)为项目配置第三方类库我们使用CocoaPods ...
- app安装位置声明
AndroidManifest.xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" ...
- Python之反射
一.引言 有时候我们会碰到类似这样的需求,就是想要执行类的某个方法,或者需要对对象的某个参数赋值,而方法名或参数名已经包装在类中并不能去顶,需要通过参数传递字符串的形式输入.在这样的情况你会选择什么样 ...
- 33、mybatis(二)
第十六章回顾SQL99中的连接查询 1)内连接 2)外连接 3)自连接 第十七章回顾hibernate多表开发 1)一对一 2)一对多 3)多对多 第十八章 mybatis一对一映射[学生与身份证] ...