OC3

it2024-10-25  20

// // main.m // OC3_字符串分割 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxueming. All rights reserved. // #import <Foundation/Foundation.h> /*2.对称字符串 传入一个字符串,将这个字符串组合成一个新的对称字符串返回 例如: 传入:@"I love you Mery" 返回:@"I love you Mery Mery you love I" */ //2, 字符串分割 @“I #am Prime # # #Optimus” //字符串分割函数 按照 @" #"字符串整体分割 //3有两个字符串@"I am a good girl hello world";@"You are a bad boy"; 将这两个字符串单词,交叠,形成新的字符串 @"I You am are a a good bad girl boy" int main(int argc, const char * argv[]) { @autoreleasepool { #if 0 //以传入的字符串整体分割字符串 //- (NSArray *)componentsSeparatedByString:(NSString *)separator; NSArray *array = [@"I love you Mery" componentsSeparatedByString:@" "]; // NSMutableArray *mulArray = [NSMutableArray arrayWithArray:array]; // [mulArray removeObject:@""]; // NSLog(@"mulArray = %@", mulArray); NSArray *dstArray = [NSArray arrayWithArray:array]; for (NSInteger i=[array count]-1; i>=0; i--) { dstArray = [dstArray arrayByAddingObject:[array objectAtIndex:i]]; } NSString *dstString =[dstArray componentsJoinedByString:@" "]; NSLog(@"dstString = %@", dstString); #endif #if 0 //以字符串整体进行分割 NSArray *array = [@"I #am Prime # # #Optimus" componentsSeparatedByString:@" #"]; NSLog(@"array = %@", array); //以字符集进行分割 NSArray *array2 = [@"I #am Prime # # #Optimus" componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" #"]]; NSLog(@"array2 = %@", array2); #endif char str1[100]={}; char str2[100]={}; scanf("%[^\n]\n%[^\n]",str1,str2); //NSLog(@"%s\n%s", str1, str2); NSString *string1 = [NSString stringWithUTF8String:str1]; NSString *string2 = [NSString stringWithUTF8String:str2]; NSArray *array1 = [string1 componentsSeparatedByString:@" "]; NSArray *array2 = [string2 componentsSeparatedByString:@" "]; NSUInteger len1 = [array1 count]; NSUInteger len2 = [array2 count]; NSArray *dstArray = [NSArray array]; NSUInteger len = len1>len2?len2:len1; for (NSInteger i=0; i<len; i++) { dstArray = [dstArray arrayByAddingObject:[array1 objectAtIndex:i]]; dstArray = [dstArray arrayByAddingObject:[array2 objectAtIndex:i]]; } NSArray *subArray =len1>len2?[array1 subarrayWithRange:NSMakeRange(len, len1-len)]:[array2 subarrayWithRange:NSMakeRange(len, len2-len)]; dstArray = [dstArray arrayByAddingObjectsFromArray:subArray]; NSString *dstString = [dstArray componentsJoinedByString:@" "]; NSLog(@"dstString = %@", dstString); } return 0; }

 

转载于:https://www.cnblogs.com/0515offer/p/4569778.html

最新回复(0)