目录
pragma mark - NSString基础
void testFirst(){
// NSString是核心处理字符串的类之一
ALog(@"1、创建一个字符串");
NSString *aSing = [NSString new];
aSing = @"aSing";
NSLog(@"显示aSing:%@",aSing);
ALog(@"2、判断字符串(英文)长度");
ALog(@"aSing长度:%ld",[aSing length]);
ALog(@"3、判断字符串(中文)长度");
NSString *aSing2 = @"我们的祖国 ";
ALog(@"aSing2长度:%ld",[aSing2 length]);
// 在OC字符串中中英文都占一个字符
ALog(@"4、当字符串为空时");
NSString *djie ;
NSLog(@"djie == null djie.length:%lu",(unsigned long)djie.length);
}
pragma mark - 字符串比较
void testCompare(NSArray *array){
ALog(@"字符串比较");
ALog(@"取出每个字符串的ASCII码值进行相互比较大小,区分大小写");
NSComparisonResult result = [array[0] compare:array[1]];
ALog(@"显示NSComparisonResult:%ld",(long)result);
switch (result) {
case NSOrderedAscending: {
// -1
ALog(@"后面的字符长度大于前面的");
break;
}
case NSOrderedSame: {
// 0
ALog(@"相等");
break;
}
case NSOrderedDescending: {
// 1
ALog(@"前面的字符长度大于后面的");
break;
}
}
}
pragma mark - 比较字符串是否相等
void testCompare2(NSArray *array){
ALog(@"字符串比较是否相等");
ALog(@"取出每个字符串相互比较,区分大小写");
BOOL checkString = [array[0] isEqualToString:array[1]];
ALog(@"显示isEqualToString:%hhd",checkString);
if (checkString) {
ALog(@"%@两个字符串相同%@",array[0],array[1]);
} else {
ALog(@"%@两个字符串不相同%@",array[0],array[1]);
}
}
pragma mark - 字符串前缀检查
void testHasPrefix(NSString *stringForHttp){
ALog(@"字符串前缀是否是http:");
ALog(@"检查字符串前缀是否包含“http”");
BOOL checkString = [stringForHttp hasPrefix:@"http:"];
ALog(@"显示hasPrefix:%hhd",checkString);
if (checkString) {
ALog(@"%@包含了http:",stringForHttp);
} else {
ALog(@"%@没包含http:",stringForHttp);
}
}
pragma mark - 字符串后缀检查
void testHasSuffix(NSString *stringForHttp){
ALog(@"字符串后缀是否是png");
ALog(@"检查字符串后缀是否包含“png”");
BOOL checkString = [stringForHttp hasSuffix:@"png"];
ALog(@"显示hasSuffix:%hhd",checkString);
if (checkString) {
ALog(@"%@包含了png:",stringForHttp);
} else {
ALog(@"%@没包含png:",stringForHttp);
}
}
pragma mark - 字符串查找定位
void testRangeOfString(NSString *stringForSearch){
ALog(@"字符串中是否包含某字符串,并确定初始位置");
ALog(@"");
NSString *stringForCertain = @"ios";
NSRange range = [stringForSearch rangeOfString:stringForCertain];
ALog(@"显示字符串“ios”首次在字符串中的位置:range.locations:%@",NSStringFromRange(range));
NSInteger allStringlength = [stringForSearch length];
if (allStringlength > range.location) {
ALog(@"查到了");
} else {
ALog(@"没查到");
}
}
pragma mark - 以指定位置开始(包括指定位置的字符),并包括之后的全部字符
void testSubstringFromIndex(NSString *stringForSearch){
ALog(@"从from起始(包括起始)截取到到尾部");
NSString *subString = [stringForSearch substringFromIndex:3];
ALog(@"显示subString:%@",subString);
}
pragma mark - 从字符串的开头一直截取到指定的位置,但不包括该位置的字符
void testSubstringToIndex(NSString *stringForSearch){
ALog(@"从起始(包括起始)截取到到from");
NSString *substringToIndex = [stringForSearch substringToIndex:3];
ALog(@"显示substringToIndex:%@",substringToIndex);
}
pragma mark - 题目:将标签内的字符串取出来你好
void testSecond(){
// 将标签内的字符串取出来你好
ALog(@"将标签内的字符串取出来你好");
NSString *way = @"将标签内的字符串取出来你dauhdiuhwdihwudqw好";
NSRange range1 = [way rangeOfString:@""];
// NSRange range2 = [way rangeOfString:@""];
// NSInteger locationRange = range2.location - range1.location-8;
way = [way substringFromIndex:range1.location];
NSInteger lengthString = way.length - 8 - 9;
NSInteger locationString = 8;
way = [way substringWithRange:NSMakeRange(locationString, lengthString)];
ALog(@"显示way:%@",way);
}
pragma mark - 替换–将*变为/
void testStringByReplacingOccurrencesOfString(NSString *stirngForSub){
NSString *subName = @"/";
ALog(@"1、显示修改前stirngForSub:%@",stirngForSub);
stirngForSub = [stirngForSub stringByReplacingOccurrencesOfString:@"*" withString:subName];
ALog(@"2、显示修改后stirngForSub:%@",stirngForSub);
}
#pragma mark - 在指定位置插入字符串
void testInsertValue(NSString *stringForTest){
// NSString *insertValue = @"我们的祖国";
// [stringForTest insertValue:nil atIndex:2 inPropertyWithKey:insertValue];
// ALog(@"显示stringForTest:%@",stringForTest);
}
pragma mark - 输出大写或者小写字符串,首字母显示
void testCaseString(){
NSString *test = @"qweqwweRtyRiopqwe";
ALog(@"显示原模板:%@",test);
ALog(@"显示字符大写:%@",[test uppercaseString]);
ALog(@"显示字符小写:%@",[test lowercaseString]);
ALog(@"显示首字母大写:%@",[test capitalizedString]);
}
pragma mark - 分解成数组
void testComponentsSeparatedByString(){
ALog(@"将有规律的字符串分解");
ALog(@"asd|tes|mmm");
NSArray *array = [@"asd|tes|mmm" componentsSeparatedByString:@"|"];
ALog(@"array:%@",array);
}
pragma mark - 主程序
int main(int argc, const char * argv[]) {
@autoreleasepool {
ALog(@"NSString大集合");
// testFirst();
// testCompare(@[@"123",@"qwe"]);
// testCompare2(@[@"QWE",@"qwe"]);
// testHasPrefix(@"http://www.baidu.com");
// testHasSuffix(@"jdiwjdoi.png");
// testRangeOfString(@"dedqiosdqoi");
// testSubstringToIndex(@"wangwenli");
// testSubstringFromIndex(@"wangwenli");
// testSecond();
// testStringByReplacingOccurrencesOfString(@"dqijd*q*wd*qiu*whdq");
// testCaseString();
testComponentsSeparatedByString();
}
return 0;
}