一:NSString 初认识
+ (void)test
{
// 第一种字符串的初始化
// NSString *str1 = @"前锋";
// 第二种(重点)字符串的拼接
NSString *str2 = [[NSString alloc] initWithFormat:@"%d钱%c枫 %@",2,'B',@"😄"];
for (int i = 0; i < 10; i ++) {
NSString *printfStr = [[NSString alloc] initWithFormat:@"我是第%d个字符",i];
NSLog(@"%@",printfStr);
}
/* // 第三种 (把c的字符串转成oc字符串)
NSString *str3 = [[NSString alloc] initWithUTF8String:"铅封"];
const char * p = [@"aoeiuv" UTF8String];
*/
/* // 第四种 (用一个字符串创建另一个字符串)
NSString *str = [[NSString alloc] initWithString:str1];
// 每一个init方法都对应一个类方法
NSString *str22 = [NSString stringWithFormat:@"少年郎"];
NSString *str33 = [NSString stringWithUTF8String:"我给你讲"];
NSString *str44 = [NSString stringWithString:str1];
*/
NSLog(@"%@",str2);
}
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// 1. 把大写转成小写
NSString *engW = [@"ABCD" lowercaseString];
// 2. 小写转成大写
NSString *engG = [@"cdesf" uppercaseString];
// 3.转成首字母大写
NSString *engF = [@"fadsfadsfdsa" capitalizedString];
// 判断字符串是不是以某个字符串开头
[@"hello,nihao" hasPrefix:@"hello"];
// 判断字符串是不是以某个字符串结尾
[@"hello,nihao" hasSuffix:@"nihao"];
}
return 0;
}
+ (void)test
{
NSString *str = @"1234567";
// 1. 获取字符串的长度
//NSInteger length = [str length];
//NSLog(@"%d",length);
// 2. 返回单个字母(Index从零开始)
unichar c = [str characterAtIndex:2];
NSLog(@"%c",c);
// 3. 字符串的比较
NSString *str2 = @"2345678";
NSComparisonResult rect = [str compare:str2];
// NSOrderedAscending = -1L,升序
// NSOrderedSame, 相等
// NSOrderedDescending 降序
if (rect == NSOrderedAscending) {
NSLog(@"右边大");
}else if (rect == NSOrderedSame){
NSLog(@"相等");
}else if (rect == NSOrderedDescending)
{
NSLog(@"左边大");
}
NSMutableString * str8 = [NSMutableString stringWithUTF8String:"hello world"];
NSLog(@"%@",str8);
}
+(void)test
{
NSString *str = @"渴了喝露水,饿了吃蚂蚱";
NSUInteger len = [str length];
NSLog(@"%lu",len);
// 从头开始截取4个长度(渴了喝露)
NSString *sub1 = [str substringToIndex:4];
// 从第二个字符开始截取 直到最后(喝露水,饿了吃蚂蚱)
NSString *sub2 = [str substringFromIndex:0];
// 位置 长度
NSRange range = {3,1};
// 从3位置开始截取 截取长度4的字符串(露水,饿)
NSString *sub3 = [str substringWithRange:range];
NSLog(@"%@",sub2);
}
+(void)test
{
// 字符串的截取
NSString *str = @"123456";
NSArray *array = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"123456"]];
// NSArray *array = [str componentsSeparatedByString:@"a"];
// NSLog(@"%@",str);
NSLog(@"%@",array);
for (id rr in array) {
NSLog(@"%@",rr);
}
//NSString *nameStr = array[0];
// NSArray *array = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@",%"]];
// 字符串的拼接
NSString *str1 = [array componentsJoinedByString:@"$"];
NSLog(@"%@",str1);
NSLog(@"%@",str);
}
+ (void)test
{
// ******* 字符串和基本数据类型的转换********
// 1.int转成NSString
NSString *str = [NSString stringWithFormat:@"%d*%d",10,12];
NSLog(@"%@",str);
// 2. NSString转成int
int strNum = [str intValue];
NSLog(@"%d",strNum);
// 其他类型类似int
// *******查找字符串的位置和范围********
NSString *strC = @"北京欢迎你";
NSRange range = [strC rangeOfString:@"欢迎"];
if (range.location == NSNotFound) {
NSLog(@"不存在");
}else
{
NSLog(@"存在");
NSLog(@"%lu---%lu",range.location,range.length);
}
}
可变字符串
#import "Test.h"
// 写一个宏 返回两个数的最大值
#define SHOW(str,str2) NSLog(@"%@-- %@",str,str2);
//#define NAX(a,b) if(a > b){return a;}else{return b;}
#define MAXX(a,b) (a > b)?a:b
@implementation Test
+ (void)test
{
// NSLog(@"%@",strM);
// SHOW(strM,strM);
// int a = 10;
// int b = 20;
// int cc = MAXX(a, b);
// 可变字符串的创建方式
// 1.
NSMutableString *strM = [[NSMutableString alloc] init];
// 可变字符串特有的方法
[strM setString:@"秦时明月"];
// 2.
NSMutableString *strM1 = [[NSMutableString alloc] initWithString:strM];
NSLog(@"%@",strM1);
// 3.
NSMutableString *strM2 = [NSMutableString stringWithUTF8String:"芈月传"];
// 从字符串后面开始拼接
[strM2 appendString:@"第一集"];
NSLog(@"%@",strM2);
//从字符串后面开始拼接 可以添加拼接的字符串
[strM2 appendFormat:@"%d--%@",3,@"呵呵"];
NSLog(@"%@",strM2);
// 在我们索引之前的位置添加
[strM2 insertString:@"广告" atIndex:0];
NSLog(@"%@",strM2);
// 删除某个位置和范围的字符
NSRange range = {1,3};
[strM2 deleteCharactersInRange:range];
NSLog(@"%@",strM2);
// 替换某个位置和范围的字符
[strM2 replaceCharactersInRange:range withString:@"天明"];
NSLog(@"%@",strM2);
}
@end
切割字符串
#import <Foundation/Foundation.h>
#import "MCJAddressBook.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
MCJAddressBook *addressBook = [[MCJAddressBook alloc] initWithString:"123,1234,dfad,12,we"];
}
return 0;
}
#import <Foundation/Foundation.h>
@interface MCJCard : NSObject
{
char _name[20];
char _corp[20];
char _phoneNumber[20];
}
- (MCJCard *)initWithName:(const char *)name corp:(const char *)corp phoneNumber:(const char *)number;
- (void)setName:(const char *)name;
- (void)setCorp:(const char *)corp;
- (void)setPhoneNumber:(const char *)phoneNumber;
@end
#import <Foundation/Foundation.h>
@interface MCJCard : NSObject
{
char _name[20];
char _corp[20];
char _phoneNumber[20];
}
- (MCJCard *)initWithName:(const char *)name corp:(const char *)corp phoneNumber:(const char *)number;
- (void)setName:(const char *)name;
- (void)setCorp:(const char *)corp;
- (void)setPhoneNumber:(const char *)phoneNumber;
@end
#import "MCJCard.h"
@implementation MCJCard
- (MCJCard *)initWithName:(const char *)name corp:(const char *)corp phoneNumber:(const char *)number
{
if(self = [super init]){
strcpy(_name, name);
strcpy(_corp, corp);
strcpy(_phoneNumber, number);
}
return self;
}
- (void)setName:(const char *)name //name = "王二小"
{
strcpy(_name, name);
}
- (void)setCorp:(const char *)corp
{
strcpy(_corp, corp);
}
- (void)setPhoneNumber:(const char *)phoneNumber
{
strcpy(_phoneNumber, phoneNumber);
}
@end
#import <Foundation/Foundation.h>
@interface MCJAddressBook : NSObject
{
//姓名,昵称,性别,手机,家庭座机,公司座机,公司,职务,所属群组,公司地址,家庭住址,生日,电子邮件,QQ号码,MSN号码,备注
char _name[20];
char _nickName[20];
char _sex[5];
char _mobileNumber[12];
char _homeTel[12];
char _companyTel[12];
char _companyName[20];
char _job[20];
char _group[20];
char _companyAdd[20];
char _homeAdd[20];
char _birthDay[20];
char _email[20];
char _QQ[20];
char _MSN[20];
char _remarks[20];
}
-(MCJAddressBook *)initWithString:(const char *)str;
@end
#import "MCJAddressBook.h"
@implementation MCJAddressBook
char * q[16] = {};
//全局变量生命周期是程序运行的全过程
//如果一个成员方法只被另一个成员方法调用,不需要被外部函数调用如main函数。则该方法只写实现,不写声明。
- (char * *)componetsStr:(char *)str withStr:(const char *)str2
{
char * p = str;
int i = 0;
while (1) {
NSLog(@"%s",p);
p = strtok(p, str2);
if(!p)
break;
//记下分割出的每一段。
q[i++] = p;
p = NULL;
}
return q;
}
- (MCJAddressBook *)initWithString:(const char *)str
{
if(self = [super init]){
//函数中的变量,在函数调用时同压栈产生,调用结束就会消失
char _str[100];
strcpy(_str, str);
//分割字符串
[self componetsStr:_str withStr:","];
// strcpy (_name, q[0]);
// strcpy (_nickName, q[1]);
// strcpy (_sex, q[2]);
// strcpy (_mobileNumber, q[3]);
// strcpy (_homeTel, q[4]);
// strcpy (_companyTel, q[5]);
// strcpy (_companyName, q[6]);
// strcpy (_job, q[7]);
// strcpy (_group, q[8]);
// strcpy (_companyAdd, q[9]);
// strcpy (_homeAdd, q[10]);
// strcpy (_birthDay, q[11]);
// strcpy (_email, q[12]);
// strcpy (_QQ, q[13]);
// strcpy (_MSN, q[14]);
// strcpy (_remarks, q[15]);
}
return self;
}
@end
切割字符串2
#import <Foundation/Foundation.h>
#import "Calculate.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
Calculate * js = [[Calculate alloc] init];
NSString * formula = @"-1+100*2/3-100" ;
NSLog(@"%ld",[js calcString:formula]);
}
return 0;
}
//实现一个函数,计算一个字符串的值,该字符串中只有+ - * /四种运算符, 没有括号。
////参数mathString:要计算的字符串;
////返回值:把计算出来的结果返回
//例如:传入:@"1+2-1+100" 返回102
//@"-1+23+100-33" 返回 89
//@"3-10*10+8/3" -95
//@"10+2/1*100-20" 190
//*/
#import <Foundation/Foundation.h>
@interface Calculate : NSObject
- (NSInteger)calcString:(NSString *)mathString;
@end
#import "Calculate.h"
@implementation Calculate
- (NSInteger)calcString:(NSString *)mathString
{
//将字符串切割成只有数字字符串和带*/的字符串
NSMutableArray * arrNum = [NSMutableArray arrayWithArray:[mathString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"+-"]]];
//如果第一个数是负数,会产生空字符串
[arrNum removeObject:@""];
//获取加减符号
NSMutableArray * arrChar = [NSMutableArray arrayWithArray:[mathString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789*/"]]];
for (NSInteger i = 0; i < arrNum.count ; i ++) {//遍历数组,将元素全部变成数字字符串
NSString * str = arrNum[i];
if ([str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*/"]].count > 1) {
//获取str中的数字字符串
NSMutableArray * arrNumSon = [NSMutableArray arrayWithArray:[str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"*/"]]];
//获取*/符号
NSMutableArray * arrCharSon = [NSMutableArray arrayWithArray:[str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789"]]];
[arrCharSon removeObject:@""];
//就算字符串str的结果
NSInteger result = [arrNumSon[0] integerValue];
for (NSInteger j = 1; j < arrNumSon.count; j ++) {
NSInteger k = [arrNumSon[j] integerValue];
if ([arrCharSon[j - 1] isEqualTo:@"*"]) {
result *= k;
}
else result /= k;
}
//将计算结果转成数字字符串存入arrNum数组中
NSString * strResult = [NSString stringWithFormat:@"%ld",result];
arrNum[i] = strResult;
}
}
//计算数组arrNum的结果
NSInteger resultSum;
if ([arrChar[0] isEqualTo:@""]) {//说明第一个数不是负数
[arrChar removeObject:@""];
resultSum = [arrNum[0] integerValue];
for (NSInteger i = 1; i < arrNum.count; i ++) {
NSInteger h = [arrNum[i] integerValue];
if ([arrChar[i - 1] isEqualTo:@"-"]) {
resultSum -= h;
}
else resultSum += h;
}
}
else{//说明第一个数是负数
[arrChar removeObject:@""];
resultSum = 0;
for (NSInteger i = 0; i < arrNum.count; i ++) {
NSInteger h = [arrNum[i] integerValue];
if ([arrChar[i] isEqualTo:@"-"]) {
resultSum -= h;
}
else resultSum += h;
}
}
return resultSum;
// NSArray * arr = [mathString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"+*/"]];
// if (arr.count == 1) {//说明都是减法
// NSArray * arrOne = [mathString componentsSeparatedByString:@"-"];
// NSInteger sum = 0;
// if ([arrOne[0] isEqualTo: @"" ]) {//说明第一个数也是负数
// for (NSInteger i = 1 ; i < arrOne.count ; i ++) {
// NSInteger num = [arrOne[i] integerValue];
// sum -= num;
// }
// return sum;
// }
// else {//说明第一个数不是是负数
// sum = [arrOne[0] integerValue];
// for (NSInteger i = 1 ; i < arrOne.count ; i ++) {// NSInteger num = [arrOne[i]
//integerValue];
// sum -= num;
// }
// return sum;
// }
//
// }
//
}
@end
//实现一个函数,计算一个字符串的值,该字符串中只有+ - * /四种运算符, 没有括号。
////参数mathString:要计算的字符串;
////返回值:把计算出来的结果返回
//例如:传入:@"1+2-1+100" 返回102
//@"-1+23+100-33" 返回 89
//@"3-10*10+8/3" -95
//@"10+2/1*100-20" 190
//*/