@fiy-fish
2015-07-18T13:33:37.000000Z
字数 3898
阅读 1328
Objective-c
// main.m
// day03-03-NSString和NSMutableString
//
// Created by Aaron on 15/7/3.
// Copyright (c) 2015年 Aaron. All rights reserved.
//
#import <Foundation/Foundation.h>
//搜索字符串
#if 0
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"abbcdeahbcfg";
/*
typedef struct _NSRange {
NSUInteger location; 索引
NSUInteger length; 长度
} NSRange;
*/
//rangeOfString: 搜索子字符串
//返回一个range
//注意: 返回的是第一次出现的范围
NSRange range = [str rangeOfString:@"abc"];
//判断是否搜索到的方法:
//一:直接通过长度
if(range.length != 0)
{
NSLog(@"搜索到");
}
else
{
NSLog(@"%ld %ld",range.location,range.length);
}
//二:利用位置
if(range.location == NSNotFound)
{
NSLog(@"没有搜索到");
}
else
{
NSLog(@"搜索到了");
}
NSString *str2 = @"abcdefg";
NSString *str3 = @"cde";
NSRange range1 = {1,3};
NSComparisonResult state = [str2 compare:str3 options:NSLiteralSearch range:range1];
NSLog(@"%ld",state);
}
return 0;
}
#endif
#if 0
int main()
{
//抽取子字符串
NSString *str = @"hello world";
//NSRange range = {2,2};
//按照范围来抽取子字符串
//- (NSString *)substringWithRange:(NSRange)range;
//str = [str substringWithRange:NSMakeRange(2, 2)];
//NSLog(@"%@",str);
//从指定的索引开始抽取子字符串
//注意: 包含索引对应的字符
//str = [str substringFromIndex:2];
//NSLog(@"%@",str);
//从头开始抽取到指定的索引为止
//注意: 不包含索引对应的字符
str = [str substringToIndex:2];
NSLog(@"%@",str);
}
#endif
/*
When I was young, I'd listen to the radio, waiting for my favorite songs. 从was提取到radio;
*/
#if 0
int main()
{
NSString *str = @"When I was young, I'd listen to the radio, waiting for my favorite songs.";
//1.确定用range来抽取
//2.location是was里面w出现的位置
NSRange range1 = [str rangeOfString:@"was"];
//3.因为location可以代表前面有多少个字节
NSRange range2 = [str rangeOfString:@"radio"];
NSString *subStr = [str substringWithRange:NSMakeRange(range1.location, range2.location-range1.location+range2.length)];
NSLog(@"%@",subStr);
}
#endif
/*
查找子字符串出现的次数
abcdefefefd
ef
*/
#if 0
int main()
{
NSString *str = @"abcdefefefd";
NSRange range = {0,0};
NSInteger times = 0;
while(1)
{
str = [str substringFromIndex:range.location+range.length];
range = [str rangeOfString:@"ef"];
if(range.location == NSNotFound)
{
break;
}
else
{
times++;
}
}
NSLog(@"出现了%ld次",times);
}
#endif
#if 0
int main()
{
NSString *str = @"sgddshjagdhasgdh";
//可以从后往前搜索
NSRange range = [str rangeOfString:@"sgd" options:NSBackwardsSearch];
//NSLog(@"%@",NSStringFromRange(range));
//可以指定范围搜索
//注意:返回的location是针对原字符串的索引
range = [str rangeOfString:@"sgd" options:NSLiteralSearch range:NSMakeRange(1, 15)];
// FOUNDATION_EXPORT NSString *NSStringFromRange(NSRange range);
//这里的NSStringFromRange(range) 是一个字符串,也就是单纯的把range按照字符串的格式输出 可以输出range(结构体)的形式
NSLog(@"%@",NSStringFromRange(range));
}
#endif
#if 0
int main()
{
NSString *str1 = @"hello world";
//获取字符串的长度
NSInteger length = [str1 length];
NSLog(@"%ld",length);
//- (unichar)characterAtIndex:(NSUInteger)index;
//获取指定索引对应的字符
//unichar 是转成unicode码之后的两个字节的数据
NSLog(@"%C",[str1 characterAtIndex:0]);
//将字符串转成大写
str1 = [str1 uppercaseString];
NSLog(@"%@",str1);
//将字符串转成小写
str1 = [str1 lowercaseString];
NSLog(@"%@",str1);
//将单词首字母转成大写
str1 = [str1 capitalizedString];
NSLog(@"%@",str1);
//@"www.baidu.com"
NSString *str2 = @"www.baidu.com";
BOOL rec = [str2 hasPrefix:@"www"];
NSLog(@"%@",rec?@"有前缀":@"无前缀");
//作业:自己实现判断是否有前缀,后缀
rec = [str2 hasSuffix:@".com"];
NSLog(@"%@",rec?@"有后缀":@"无后缀");
// /users/Aaron/desktop/test1.txt
NSString *str3 = @"1.23";
//将字符串转成基本类型的数据
float temp = [str3 floatValue];
NSLog(@"%.2f",temp);
//注意:转换的时候,遇到不是数字的字符就停止
NSString *str4 = @"a1001";
int t = [str4 intValue];
NSLog(@"%d",t);
const char *s = [str4 UTF8String];
NSLog(@"%s",s);
}
#endif
int main()
{
//可变字符串
NSMutableString *str1 = [[NSMutableString alloc] init];
//init对于可变字符串是有意义的
NSMutableString *str2 = [NSMutableString string];
NSMutableString *str3 = [NSMutableString stringWithString:@"hello"];
NSLog(@"%@",str3);
NSString *str4 = [[NSString alloc] initWithString:str3];
//可变字符串仅仅是在不可变的基础上增加了 增删改几种方法
//增
//追加字符串
[str3 appendString:@"xxx"];
NSLog(@"%@",str3);
//追加格式
[str3 appendFormat:@"teacherName:%@",@"xiaoxin"];
NSLog(@"%@",str3);
//插入
[str3 insertString:@"..." atIndex:0];
NSLog(@"%@",str3);
//删除
//删除指定范围内的字符
[str3 deleteCharactersInRange:NSMakeRange(0, 8)];
NSLog(@"%@",str3);
//修改
[str3 replaceCharactersInRange:NSMakeRange(3, 7) withString:@""];
NSLog(@"%@",str3);
//直接设置字符串内容
[str3 setString:@""];
NSLog(@"--%@",str3);
}