Main.m
#import <Foundation/Foundation.h>
#import "NSString+Extesion.h"
int main(int argc, const char * argv[])
{
// 1、截取字符串”20|http://www.baidu.com”中,”|”字符前面和后面的数据,分别输出
//方式一:
NSString *str1 = @"20|http://www.baidu.com";
NSRange rg = [str1 rangeOfString:@"|"];
NSString *string1 = [str1 substringToIndex:rg.location];
NSString *string2 = [str1 substringFromIndex:rg.location + 1];
NSLog(@"string1:%@ string2:%@",string1,string2);
//方式二:
NSArray *arrary = [str1 componentsSeparatedByString:@"|"];
NSLog(@"前面的字符串:%@ 后面的字符串:%@",arrary[0],arrary[1]);
// 2、从字符串@“aBcD_EfGk”中取出EfGk,并让大写字母转成小写、小写改成大写,输出eFgK
NSString *str2 = @"aBcD_EfGk";
NSArray *arrary2 = [str2 componentsSeparatedByString:@"_"];
//EfGk
NSString *string3 = arrary2[1];
// string3 = [string3 lowercaseString];
// NSLog(@"string3:%@",string3);
NSMutableString *mutStr = [[NSMutableString alloc] init];
for (int i=0; i<string3.length ; i++) {
//取得指定下标的元素
unichar c = [string3 characterAtIndex:i];
//判断c是否是小写的
if (c >= 'a' && c <= 'z') {
//将c改成大写
c = c - 32;
}
//判断c是否是大写的,注意加else
else if (c >= 'A' && c <= 'Z') {
//将c改成小写
c = c + 32;
}
//拼接字符串
[mutStr appendFormat:@"%c",c];
}
NSLog(@"mutStr:%@",mutStr);
//3、创建一个类,功能:给定⼀一个字符串,判断字符串中是否含有“png”,如果有就删除。
NSString *string4 = @"wertyuio---pn-g----dfghjkl";
string4 = [string4 textPng];
NSLog(@"string4:%@",string4);
return 0;
}
NSString+Extesion.h
@interface NSString (Extesion)
//3、创建一个类,功能:给定⼀一个字符串,判断字符串中是否含有“png”,如果有就删除。
- (NSString *)textPng;
NSString+Extesion.m
- (NSString *)textPng
{
//查找png
NSRange rg = [self rangeOfString:@"png"];
//如果找到png
if (rg.location != NSNotFound)
{
//删除字符串png
NSArray *arrary = [self componentsSeparatedByString:@"png"];
self = [arrary componentsJoinedByString:@""];
}
return self;
}