[程序员日记]16题了解OC字符串
-
查找一个字符串2在字符串1中出现的次数
-(NSUInteger)countOfString:(NSString *)str1 withSubstring:(NSString *)str2 { NSArray * arr = [str1 componentsSeparatedByString:str2]; return [arr count] -1 ; }
-
输入一段字符串,统计其中的单词个数
比如:输入 I like China 输出:3
-(NSUInteger)countOfWordInString:(NSString *)str
{
NSArray * arr = [str componentsSeparatedByString:@" "];
int count = 0;
for (NSString * obj in arr) {
if (![obj isEqualToString:@""] ) {
count++;
}
}
return count;
} -
判断字符串str是否是合法的C变量,比如不能数字开头,只能字符串和等开头
#define REGULAROFIFFOR3_1 (c >= 'a' && c<='z')||(c>='A'&&c<='Z')||(c=='')
#define REGULAROFIFFOR3_2 (c >= 'a' && c<='z')||(c>='A'&&c<='Z')||(c>='1'&&c<='9')||(c=='_')
-(BOOL)isCValidVar:(NSString *)str
{
char c = [str characterAtIndex:0];
if (!(REGULAROFIFFOR3_1)) {
return NO;
}
for(int i = 1; i < [str length]; i++)
{
c = [str characterAtIndex:i];
if (!(REGULAROFIFFOR3_2)) {
return NO;
}
}
return YES;
} -
数组排序 arr数组中包含的是NSString字符串单词
在英语类的App很多时候按照最后一个单词进行排序.
将数组arr进行按照单词 最后一个字母进行升序排序(注意 最后一个字母),比如arr中是(hello, world,apple,demo)
返回是(world, hello,apple, demo) 如果最后一个字母相同,就按照倒数第二个,以此类推-(NSArray *)sortWord:(NSArray *)arr { NSMutableArray * marr = [NSMutableArray arrayWithArray:arr]; [marr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { long objLen1 = [obj1 length]; long objLen2 = [obj2 length]; long top = objLen1<objLen2 ? objLen1:objLen2; for ( int i = 1 ; i < top; i++) { NSComparisonResult result = [[obj1 substringWithRange:NSMakeRange(objLen1-i, 1)] compare:[obj2 substringWithRange:NSMakeRange(objLen2-i, 1)]]; if (result != 0) { return result; } } return NSOrderedSame; }]; return marr; }
-
取得文件的扩展名
比如 传入字符串 @"/home/apple/oc.txt" 返回:@"txt"
传入字符串 @"/usr/apple/ios.m" 返回:@"m"
- (NSString *)extensionOfFilePath:(NSString *)path
{
NSArray * arr = [path componentsSeparatedByString:@"."];
return [arr objectAtIndex:[arr count]-1];
}
6.将一个字符串数组中的元素组合成一个合法路径已知一个数组存放的是录
名字,要求组合成一个合法路径
比如:数组中存放的是 home apple iOS
返回 /home/apple/iOS
- (NSString *)joinPathOfComponents:(NSArray *)comp
{
NSMutableString * mstr = [[NSMutableString alloc]init];
for (id obj in comp) {
[mstr appendFormat:@"/%@",obj];
}
return mstr;
}
7.用NSLog打印九九乘法表
- (void)print99Table
{
for(int i = 0 ; i < 10; i++)
{
for (int j = 0 ; j < 10 ; j++) {
NSLog(@"%d x %d = %d",i,j,i*j);
}
}
}
8.将C++的标识符转成OC的标识符
C++的标识符和OC一样由数字字母下划线组成,打头的不是数字。当标识符超过一个单词,C++采用全字母小写,单词间用下划线连接的书写规范,如:apple_ios
OC采用除第一个单词外,其余单词首字母大写的书写规范,如:appleios
传入C++标识符,返回OC标识符
- (NSString *)objcIdentifierFromCppIdentifier:(NSString *)idf
{
NSArray * arr = [idf componentsSeparatedByString:@"_"];
NSMutableString * mstr = [[NSMutableString alloc]init];
for (id obj in arr) {
if ([arr indexOfObject:obj] != 0 ) {
NSMutableString * tmstr = [NSMutableString stringWithString:obj];
[tmstr replaceCharactersInRange:NSMakeRange(0, 1) withString:[NSString stringWithFormat:@"%c",[tmstr characterAtIndex:0]-32]];
[mstr appendString:tmstr];
}else
{
[mstr appendString:obj];
}
}
return mstr;
}
9、剔除字符串中的全部空格
传入: @"welcome to china”
返回:@"welcometochina”
-(NSString *)stringWithoutSpaceInString:(NSString *)string
{
NSArray * arr = [string componentsSeparatedByString:@" "];
NSMutableString * mstr = [[NSMutableString alloc]init];
for (id obj in arr) {
[mstr appendString:obj];
}
return mstr;
}
10、计算两个字符串所表示数字的和
传入:@"123" @"459"
返回:@"482"
-(NSString *)sumOfNumber:(NSString *)string1 andNumber:(NSString *)string2
{
return [NSString stringWithFormat:@"%.0lf",[string1 doubleValue]+[string2 doubleValue]];
}
11.计算两个字符串所表示数字的乘积
传入:@"15" @"15"
返回:@"255"
-(NSString *)productOfNumber:(NSString *)string1 andNumber:(NSString *)string2
{
return [NSString stringWithFormat:@"%.0lf",[string1 doubleValue]*[string2 doubleValue]];
}
12、将字符串后移
将字符串向右移动指定位数,首尾循环
如:string传入@ "welcometochina”, bits传入5
返回:@“chinawelcometo”
-(NSString *)displacemetString:(NSString *)string forBits:(NSUInteger)bits
{
NSMutableString * mstr = [[NSMutableString alloc]initWithString:string];
NSString * str = [string substringFromIndex:[string length]-bits];
[mstr replaceCharactersInRange:NSMakeRange(bits, [mstr length]-bits) withString:[mstr substringWithRange:NSMakeRange(0, [mstr length]-bits)]];
[mstr replaceCharactersInRange:NSMakeRange(0, bits) withString:str];
return mstr;
}
13、传入两个字符串,找出第二个字符串中每个字符在第一个字符串中出现的次数
传入:@"hello" @"abel"
返回:@"a:0 b:0 e:1 l:2"
- (NSString *)timesInString:(NSString *)str1 withCharactersInString:(NSString *)str2
{
NSMutableString * mstr = [[NSMutableString alloc]init];
for(int i = 0 ; i < [str2 length]; i++)
{
NSString * charStr = [str2 substringWithRange:NSMakeRange(i, 1)];
NSArray * tarr = [str1 componentsSeparatedByString:charStr];
[mstr appendFormat:@"%@:%lu ",charStr,[tarr count]-1];
}
return mstr;
}
14.按照新规则比较两个字符串的大小
比较两个字符串的大小,首先比较两个字符串最后一个字符,如果最后一个字符相等,比较倒数第二个字符,如果字符相同,更长的大,依次类推,s1大返回1,s2大返回-1,相等返回0
传入@"abc" @"abd" 返回-1
传入@"abc" @"zabc" 返回-1
传入@"abc" @"abc" 返回0
-(int)reverseWordsInString:(NSString *)str1 andOtherString:(NSString *)str2
{
int length = (int)([str2 length]>[str1 length]?[str1 length]:[str2 length]);
for (int i = length -1; i >= 0; i--) {
if ([str1 characterAtIndex:i] > [str2 characterAtIndex:i]) {
return 1;
}else if([str1 characterAtIndex:i] < [str2 characterAtIndex:i]){
return -1;
}
}
return 0;
}
15.将字符串中单词按照出现次数排序,每个单词只出现一次,源字符串中单词用下划线连接,生成字符串也应用下滑线连接
如传入:@"good_good_study_good_study"
返回:@"good_study"
- (NSString *)sortStringByNumberOfWordsFromString:(NSString *)str
{
NSArray * arr = [str componentsSeparatedByString:@"_"];
NSMutableArray * tgtArr = [[NSMutableArray alloc]init];
NSMutableDictionary * countDict = [[NSMutableDictionary alloc]init];
int count = 0;
for (id obj in arr) {
if (![tgtArr containsObject:obj]) {
[tgtArr addObject:obj];
}
}
for (id obj in tgtArr) {
for (id obj2 in arr) {
if (obj == obj2) {
count++;
}
}
[countDict setObject:[[NSNumber alloc]initWithInt:count] forKey:obj];
count = 0;
}
[tgtArr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [countDict[obj1] intValue] <[countDict[obj2] intValue];
}];
return [tgtArr componentsJoinedByString:@"_"];
}
16.传入两个字符串,第二个字符串是一串连续数字,将第一个字符串(不超过9个字符),按照第二个字符串中所标示顺序重新排序
传入:@"abcdef" @"465231"
返回:@"dfebca”
-(NSString *)sortString:(NSString *)string asOrder:(NSString *)order
{
NSMutableString * mstr = [[NSMutableString alloc]initWithString:string];
NSMutableString * mstr2 = [[NSMutableString alloc]initWithString:order];
for (int i = 0; i < [mstr length]; i++) {
char c = [mstr characterAtIndex:i];
for (int j = 0; j < [mstr2 length]; j++) {
if ([mstr2 characterAtIndex:j] == i+49 ) {
[mstr2 replaceCharactersInRange:NSMakeRange(j, 1) withString:[NSString stringWithFormat:@"%c",c]];
break;
}
}
}
return mstr2;
}
文章优先发表于:http://keyliu.com
转载请注明出处。