您的位置:首页 > 其它

OC: NSString、NSArray、NSNumber

2015-08-13 09:28 411 查看
数组参考: 参考1 参考2 参考3

//字符串

//1.获取字符串的长度:

//表情符号最少占两个字节

NSString * s = @"中文字符串😃nothing is imposible";

unsigned long len = [s length];

NSLog(@"%lu",len);

//字符串的前缀 后缀 hasPrefix hassuffix

NSString * strUrl = @"http://www.baidu.com";

//判断字符串是否是以某字符串开头,如果是则返回 yes 否则返回 No

BOOL isStart = [strUrl hasPrefix:@"http"];

//用 hasSuffix 来判断某个字符串是否是以某个字符串为结尾

BOOL isEnd = [strUrl hasSuffix:@"com"];

//判断网址是否正确

if(isEnd && isStart){

NSLog(@"这个网址是正确的");

}

NSLog(@"%d",isEnd);

NSLog(@"%d",isStart);

//字符串里的某一部分(子字符串)

// 输入一个字符串判断是否是以 WWW 开头

char temp[10] = {};

printf("请输入一个字符串");

scanf("%s",temp);

NSString * hh = [NSString stringWithUTF8String:temp];

BOOL ist = [hh hasPrefix:@"www"];

if (ist) {

NSLog(@"是以WWW开头");

}

}

2.查找一个字符串里是否有另一个字符串

//字符串搜索 获取字符串里面的子字符串 搜索字符串的范围
//通过 rangeOfString 查找字符串中是否含有某个字符串
NSString * string1 = [[NSString alloc]initWithFormat:@"、 NN、BB、妞妞、哈哈、淘气、铅笔%@",@"中华"];
NSRange range = [string1 rangeOfString:@"NN"];
NSLog(@"下标位置:%lu 长度:%lu",range.location , range.length);
// NSRange是一个结构体,有两个成员变量,有一个 location length ,通过location  确定字符串起始位置的下标,从这个下标开始获取 length 长度的子字符串
if (range.length == 0) {
NSLog(@"没有找到该字符串");
}
else{
NSLog(@"找到了该字符串");
}

查找一个字符串里是否有另一个字符串


3.字符串截取

/*
//         substringFromIndex: 从index开始一直打印到字符串的最后包含的下标(包含 index)
//         substringToIndex: 从字符串开始一直到下标 index (布包含 index)
*/
//字符串截取
NSString * divString  = @"what a beautfiul day";
NSString * newString = [divString substringFromIndex:1];
NSString * newString1 = [divString substringToIndex:7];//下标是7的字符并没有打印出来
NSLog(@"%@",newString);
NSLog(@"%@",newString1);
NSRange starToEnd = NSMakeRange(0, 4);//这是我们获取的范围 我们用到了NSMakeRange(loc ,len) 方法
//按 alt 键可以了解 类的说明
NSString * newString2 = [divString substringWithRange:starToEnd];
NSLog(@"%@",newString2);
}

字符串截取


substringFromIndex 从开始的下标到最后的下标(包含最后的下标)

substringToIndex 从字符串开始一直到下标 index (布包含 index)

substringWithRange

NSRange starToEnd = NSMakeRange(0, 4);//这是我们获取的范围 我们用到了NSMakeRange(loc ,len) 方法

4. 字符串拼接

//stringByAppendingString: 方法就是在一个给定的字符串后面拼接一个字符串

NSString * append = [divString stringByAppendingString:@"我是拼接来的"];

NSLog(@"%@",append);

//stringByAppendingFormat: 在给定的字符串后面去拼接上一个给定格式的字符串

NSString * appendformat = [divString stringByAppendingFormat:@"he %d he",12];

NSLog(@"%@",appendformat);

// stringByReplacingOccurrencesOfString: : 给定的字符串 全部替换(有几个相同字符串都去替换)

NSString * replace = [divString stringByReplacingOccurrencesOfString:@"day" withString:@"(我是stringByReplacingOccurrencesOfString方法替换的)"];

NSLog(@"%@",replace);

//方法名字是 stringByReplacingOccurrencesOfString: withString:

把给定的字符串中所有要替换的字符串给替换掉,注意是所有的要替换掉的,本例中就是 day ,如果出现 好多个 day 那就吧 好多的 day 都替换掉

// stringByReplacingCharactersInRange:NSMakeRange(0, 4) withString:@"it's "] 吧位置从0开始长度为4的字符串 替换为 it's

[divString stringByReplacingCharactersInRange:NSMakeRange(0, 4) withString:@"it's "]; //给固定的范围去替换

//多多总结报错的原因

3.字符串比较

/*
字符串的比较 compare
*/
NSString *cmp1 = @"hello";
NSString *cmp2 = @"how";
NSComparisonResult result = [cmp1 compare:cmp2];//点住compare 查看compare的返回值类型  NSComparisonResult  。有三个值  -1 0 1
//这里的比较与c 语言里的一样,当比较到第二个字符时就出现结果
switch (result) {
case -1://升序
//            case NSOrderedAscending://升序
NSLog(@"cmp2大于cmp1");
break;

case NSOrderedSame:
NSLog(@"cmp1大于cmp2");
break;

case NSOrderedDescending:
NSLog(@"cmp1等于cmp2");
break;
default:
break;
}
}


/*
字符串的比较 compare
*/
NSString *cmp1 = @"hello";
NSString *cmp2 = @"how";
NSComparisonResult result = [cmp1 compare:cmp2];//点住compare 查看compare的返回值类型  NSComparisonResult  。有三个值  -1 0 1
//这里的比较与c 语言里的一样,当比较到第二个字符时就出现结果
switch (result) {
case -1://升序
//            case NSOrderedAscending://升序
NSLog(@"cmp2大于cmp1");
break;

case NSOrderedSame:
NSLog(@"cmp1大于cmp2");
break;

case NSOrderedDescending:
NSLog(@"cmp1等于cmp2");
break;
default:
break;
}
/*
比较两个字符串是否相等
*/
BOOL rigth = [cmp1 isEqualToString:@"hello"];
NSLog(@"%d",rigth);
/*
字符串的字母的大写 小写
*/
NSString * tmpStr = @"i will be the Best";
//想让他们全部大写 或者 小写
NSString * newTem = [tmpStr lowercaseString];//全部小写
NSLog(@"%@",tmpStr);
NSLog(@"%@",newTem);
NSString * newTem2 = [tmpStr uppercaseString];//全部大写
NSLog(@"%@",newTem2);


//首字母大写(所有的子字符串首字母大写) capitalizedString 方法

NSString * newTem3 = [tmpStr capitalizedString];

NSLog(@"%@",newTem3);

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
@autoreleasepool {
//练习: 输入一个字符串判断是否是以 WWW 开头
/*
char temp[10] = {};
printf("请输入一个字符串");
scanf("%s",temp);
NSString * hh = [NSString stringWithUTF8String:temp];
BOOL ist = [hh hasPrefix:@"www"];
if (ist) {
NSLog(@"是以WWW开头");
}
*/

/*
//字符串搜索 获取字符串里面的子字符串 搜索字符串的范围
//通过 rangeOfString 查找字符串中是否含有某个字符串
NSString * string1 = [[NSString alloc]initWithFormat:@"、 英杰、刘娴、妞妞、田园、淘气、铅笔%@",@"中华"];
NSRange range = [string1 rangeOfString:@"刘娴"];
NSLog(@"下标位置:%lu 长度:%lu",range.location , range.length);
// NSRange是一个结构体,有两个成员变量,有一个 location length ,通过location  确定字符串起始位置的下标,从这个下标开始获取 length 长度的子字符串
if (range.length == 0) {
NSLog(@"没有找到该字符串");
}
else{
NSLog(@"找到了该字符串");
}
*/

/*//字符串截取
//         substringFromIndex: 从index开始一直打印到字符串的最后包含的下标(包含 index)
//         substringToIndex: 从字符串开始一直到下标 index (布包含 index)
NSString * divString  = @"what a beautfiul day";
NSString * newString = [divString substringFromIndex:1];
NSString * newString1 = [divString substringToIndex:7];//下标是7的字符并没有打印出来
NSLog(@"%@",newString);
NSLog(@"%@",newString1);
NSRange starToEnd = NSMakeRange(0, 4);//这是我们获取的范围 我们用到了NSMakeRange(loc ,len) 方法
//按 alt 键可以了解 类的说明
NSString * newString2 = [divString substringWithRange:starToEnd];
NSLog(@"%@",newString2);
//stringByAppendingString: 方法就是在一个给定的字符串后面拼接一个字符串
NSString * append = [divString stringByAppendingString:@"我是拼接来的"];
NSLog(@"%@",append);
//stringByAppendingFormat: 在给定的字符串后面去拼接上一个给定格式的字符串
NSString * appendformat = [divString stringByAppendingFormat:@"he %d he",12];
NSLog(@"%@",appendformat);
//        stringByReplacingOccurrencesOfString: : 给定的字符串 全部替换(有几个相同字符串都去替换)
NSString * replace = [divString  stringByReplacingOccurrencesOfString:@"day" withString:@"(我是stringByReplacingOccurrencesOfString方法替换的)"];//把所有的day 都替换掉
NSLog(@"%@",replace);
//        stringByReplacingCharactersInRange:NSMakeRange(0, 4) withString:@"it's "] 吧位置从0开始长度为4的字符串 替换为 it's
[divString stringByReplacingCharactersInRange:NSMakeRange(0, 4) withString:@"it's "];//给固定的范围去替换
*/

/*
字符串的比较 compare
*/
NSString *cmp1 = @"hello";
NSString *cmp2 = @"how";
NSComparisonResult result = [cmp1 compare:cmp2];//点住compare 查看compare的返回值类型  NSComparisonResult  。有三个值  -1 0 1
//这里的比较与c 语言里的一样,当比较到第二个字符时就出现结果
switch (result) {
case -1://升序
//            case NSOrderedAscending://升序
NSLog(@"cmp2大于cmp1");
break;

case NSOrderedSame:
NSLog(@"cmp1大于cmp2");
break;

case NSOrderedDescending:
NSLog(@"cmp1等于cmp2");
break;
default:
break;
}
/*
比较两个字符串是否相等
*/
BOOL rigth = [cmp1 isEqualToString:@"hello"];
NSLog(@"%d",rigth);
/*
字符串的字母的大写 小写
*/
NSString * tmpStr = @"i will be the Best";
//想让他们全部大写 或者 小写
NSString * newTem = [tmpStr lowercaseString];//全部小写
NSLog(@"%@",tmpStr);
NSLog(@"%@",newTem);
NSString * newTem2 = [tmpStr uppercaseString];//全部大写
NSLog(@"%@",newTem2);
//首字母大写(所有的子字符串首字母大写)
NSString * newTem3 = [tmpStr capitalizedString];
NSLog(@"%@",newTem3);
//字符串中的数字转化为基本的数字
NSString *numStr = @"234";
NSInteger newNum = [numStr integerValue];//得到长整型数字
NSLog(@"%ld",newNum);
float newNum0  = [numStr floatValue];//转换为浮点型数字
NSLog(@"%.2f",newNum0);
int newNum1 = [numStr intValue];//转化为int整数
NSLog(@"%d",newNum1);
//        //把基本整型数字 转化为对象?????
//        int a = [numStr intValue];

//可变字符串 NSMutableString 是不可变字符串 NSString 的子类 以后的任何一个集合里面 可变的都是不可变的子类
NSMutableString * mutStr = [NSMutableString stringWithCapacity:1];
NSMutableString * mutableStr = [[NSMutableString alloc]initWithCapacity:10];
NSLog(@"%@",mutStr);//是空的不打印
//        - (void)appendFormat:(NSString *)format
//为字符串进行添加 //append 就是附加的意思
[mutStr appendString:@"我是appendString方法"];
NSLog(@"%@",mutStr);
[mutStr appendFormat:@"%d",10];
NSLog(@"%@",mutStr);
//删除给定范围的字符串
[mutStr deleteCharactersInRange:NSMakeRange(0, 3)];
NSLog(@"%@  ->已经删除了范围在(0,3)",mutStr);
//插入字符串
[mutStr insertString:@"-我是insertString插入的方法-" atIndex:0];
NSLog(@"%@",mutStr);
//对于不懂的方法去试一试
//replaceCharactersInRange 替换给定范围的字符串
[mutStr replaceCharactersInRange:NSMakeRange(6, 10) withString:@"-12-"];
NSLog(@"%@",mutStr);
//     mutStr replaceOccurrencesOfString:<#(NSString *)#> withString:<#(NSString *)#> options:<#(NSStringCompareOptions)#> range:<#(NSRange)#>
//全部替换字符串
[mutStr setString:@"NEW String"];
NSLog(@"%@",mutStr);
}return 0;
}


关于 stringWithUTF8String 的使用:

stringWithUTF8String:(char*)str
这个方法主要是用在从外部存储文件读取C字符串后,转换成界面上表示用的UTF8文字.

此方法使用的时候有个限制,
str的一定要以'\0'结束. //

sample code:

if((int)buffer[bufferSize] != 0)
buffer[20] = 0;

[NSString stringWithUTF8String:buffer]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: