直接上代码,排版有些乱,有时间再整理
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
// 1.将file://baidu.jpg中的file替换为http
// NSMutableString *str = [NSMutableString stringWithString:@"file://baidu.jpg"];
NSMutableString *str = [NSMutableString string];
// 设置整个字符串的内容
str.string = @"file://baidu.jpg";
// 替换
// NSRange range = NSMakeRange(0, str.length);
NSLog(@"%@", str);
// [str replaceOccurrencesOfString:@"file" withString:@"http" options:0 range:range];
[str replaceCharactersInRange:[str rangeOfString:@"file"] withString:@"http"];
NSLog(@"%@", str);
// 将字符串拼接到最后面
// [str appendString:@"abc"];
// 设置整个字符串的所有内容
// str.string = @"def";
// 这种方法会产生新的字符串对象
// NSString *str = @"file://baidu.xxxxxx";
//
// NSString *newStr = [str stringByReplacingOccurrencesOfString:@"file" withString:@"http"];
//
// NSLog(@"str=%@", str);
// NSLog(@"newStr=%@", newStr);
}
return 0;
}
void insertString()
{
NSMutableString *str = [NSMutableString string];
[str appendString:@"github"];
[str insertString:@"http://" atIndex:0];
NSLog(@"%@", str);
}
/**
不建议采取(会产生很多临时的字符串对象)
*/
void appendString4()
{
// 1.将10个itcast拼接起来, 中间用空格隔开
// 目标字符串
NSString *destStr = @"";
// 拼接字符串
for (int i = 0; i<10; i++) {
destStr = [destStr stringByAppendingString:@" baidu"];
}
destStr = [destStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSLog(@"-%@-", destStr);
}
void appendString3()
{
// 1.将10个baidu拼接起来, 中间用空格隔开
NSMutableString *destStr = [NSMutableString string];
for (int i = 0; i<10; i++) {
[destStr appendString:@" baidu"];
}
// 2.删除最后一个空格
[destStr deleteCharactersInRange:NSMakeRange(0, 1)];
// [destStr deleteCharactersInRange:NSMakeRange(destStr.length - 1, 1)];
NSLog(@"--%@--", destStr);
}
void deleteString()
{
NSMutableString *str = [NSMutableString string];
[str appendString:@"http://itcast"];
NSLog(@"初始化:%@", str);
// [str deleteCharactersInRange:NSMakeRange(0, 7)];
// 结合rangeOfString和deleteCharactersInRange方法能准确删除某个固定的字符串
NSRange range = [str rangeOfString:@"itcast"];
[str deleteCharactersInRange:range];
NSLog(@"删除后:%@", str);
}
void appendString2()
{
// 1.将10个itcast拼接起来, 中间用空格隔开, 最后写入文件中
NSString *substr = @"itcast";
NSMutableString *destStr = [NSMutableString string];
for (int i = 0; i<10; i++) {
// if (i != 0) {
if (i) { // 只有i不等于
[destStr appendString:@" "];
}
[destStr appendString:substr];
}
[destStr writeToFile:@"/Users/apple/Desktop/dest.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
void appendString()
{
// 不可变
NSString *str = @"Jack";
// 可变
NSMutableString *str2 = [NSMutableString string];
[str2 appendString:@"Rose"];
[str2 appendString:@" love "];
[str2 appendString:@"Jack."];
int age = 20;
[str2 appendFormat:@"My age is %d", age];
NSLog(@"%@", str2);
}
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *str = @"hkjfaslhjfkfkj会尽快发生空间hjksdkjhfhkjdsfhk;ds;khfk;hds";
[str writeToFile:@"/Users/apple/Desktop/str.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
}
return 0;
}
void stringCreate2()
{
// 1.从一个URL中读取字符串内容
// 协议头://主机域名/路径
// file://192.168.20.25/Users/apple/Desktop/test.txt
// file:///Users/apple/Desktop/test.txt
// NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
// NSURL *url = [NSURL URLWithString:@"file:///Users/apple/Desktop/test.txt"];
// fileURLWithPath创建出来的URL默认就是file协议
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/Desktop/test.txt"];
NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", str);
}
void stringCreate()
{
// 1.直接赋值 这两个是同一个字符串 存放在栈当中的(栈中的变量每次创建不会重新分配内存 NSString *str = @"Jack";
NSString *str3 = @"Jack";
// 2.格式化 这两个不是同一个字符串 存放在堆空间当中的(堆空间的变量每次创建都会重新分配内存
NSString *str2 = [NSString stringWithFormat:@"Jack"];
NSString *str4 = [NSString stringWithFormat:@"Jack"];
//通过P打印内存地址
NSLog(@"%p - %p", str2, str4);
// NSString *str2 = [[NSString alloc] initWithFormat:@"my age is %d", 10];
// [NSString stringWithFormat:<#(NSString *), ...#>];
// 3.从文件中读取
NSError *eror = nil;
NSString *str5 = [NSString stringWithContentsOfFile:@"/Users/apple/Desktop/test.txt" encoding:NSUTF8StringEncoding error:&eror];
if (eror != nil) {// 读取失败
NSLog(@"读取失败,失败原因是:%@", [eror localizedDescription]);
} else {// 读取成功
NSLog(@"读取成功,文件内容是:%@", str5);
}
}
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSString *str = @"adc";
NSString *str2 = @"ADc";
// str > str2 NSOrderedDescending
// str < str2 NSOrderedAscending
// str == str2 NSOrderedSame
NSComparisonResult result = [str compare:str2];
switch (result) {
case NSOrderedDescending:
NSLog(@"str > str2");
break;
case NSOrderedAscending:
NSLog(@"str < str2");
break;
case NSOrderedSame:
NSLog(@"str == str2");
break;
}
}
return 0;
}
void str(){
NSString *str = @"android:///Users/apple/android/jkl/test.jpg";
if ([str hasPrefix:@"http"]) {
NSLog(@"远程网络资源");
} else {
NSLog(@"非远程网络资源");
}
if ([str hasSuffix:@"jpg"]) {
NSLog(@"jpg资源");
} else {
NSLog(@"非jpg资源");
}
NSRange range = [str rangeOfString:@"android" options:NSBackwardsSearch];
NSLog(@"range.location=%zd %zd", range.location, range.length);
// 如果找不到对应的字符串, range.location==NSNotFound, range.length==0
// if (range.location == NSNotFound) { // 没有找到
if (range.length == 0) {// 没有找到
NSLog(@"str里面没有包含android");
} else {
NSLog(@"str里面有包含android");
}
// NSLog(@"%d", [str isEqualToString:str2]);
// NSString *str = @"JackfsdfJH";
// NSString *str2 = [str capitalizedString];
//
// NSLog(@"str=%@", str);
//
// NSLog(@"str2=%@", str2);
}
void compare()
{
NSString *str = @"adc";
NSString *str2 = @"ADc";
// str > str2 NSOrderedDescending
// str < str2 NSOrderedAscending
// str == str2 NSOrderedSame
NSComparisonResult result = [str compare:str2];
switch (result) {
case NSOrderedDescending:
NSLog(@"str > str2");
break;
case NSOrderedAscending:
NSLog(@"str < str2");
break;
case NSOrderedSame:
NSLog(@"str == str2");
break;
}
}
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
}
return 0;
}
void stringTrimUpper()
{
NSString *str = @"HTTP://ios.itcast.cn/IOS/images/content_25.jpg";
NSString *newStr = [str stringByTrimmingCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
NSLog(@"%@", newStr);
}
void stringTrimSpace()
{
NSString *str = @" http://ios.itcast.cn ios/images/ content_25.jpg ";
// trim : 去除头尾
// whitespaceCharacterSet : 空格
NSCharacterSet *set = [NSCharacterSet whitespaceCharacterSet];
NSString *newStr = [str stringByTrimmingCharactersInSet:set];
NSLog(@"--%@--", str);
NSLog(@"--%@--", newStr);
}
void string2int()
{
NSString *str = @"108";
NSString *str2 = @"200.5";
// str.intValue ---> 108
// int sum = str.intValue + str2.intValue;
double sum = str.intValue + str2.doubleValue;
NSLog(@"%f", sum);
}
void replaceString2()
{
NSString *str = @" http:** ios.baidu.cn *ios*images* content_25.jpg ";
// 1.把空格消掉
NSString *newStrWithoutSpace = [str stringByReplacingOccurrencesOfString:@" " withString:@""];
// 2.把*换成/
NSString *destStr = [newStrWithoutSpace stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
NSLog(@"str = %@", str);
NSLog(@"newStrWithoutSpace = %@", newStrWithoutSpace);
NSLog(@"destStr = %@", destStr);
}
/**
将http:**ios.itcast.cn*ios*images*content_25.jpg中的*替换为/
*/
void replaceString()
{
NSString *str = @"http:**ios.baidu.cn*ios*images*content_25.jpg";
// Replacing : 替换
// Occurrences : 出现
// 用/替换所有出现的*
NSString *newStr = [str stringByReplacingOccurrencesOfString:@"*" withString:@"/"];
NSLog(@"%@", newStr);
}
/**
用3种方法将下面字符串中的中文截取出来
*/
void substring()
{
NSString *str = @"<itt>人工智能</itt>";
// 不建议采取
// NSRange range;
// range.location = 8;
// range.length = 4;
// NSString *substr = [str substringWithRange:range];
// 1.方法1
// NSRange range;
// range.location = [str rangeOfString:@">"].location + 1;
// range.length = [str rangeOfString:@"</"].location - range.location;
/**
typedef struct _NSRange {
NSUInteger location;
NSUInteger length;
} NSRange;
*/
NSUInteger loc = [str rangeOfString:@">"].location + 1;
NSUInteger len = [str rangeOfString:@"</"].location - loc;
// NSRange range = {loc, len};
// NSRange range = {.length = len, .location = loc};
NSRange range = NSMakeRange(loc, len);
NSString *substr = [str substringWithRange:range];
NSLog(@"%@", substr);
}