字符串作业题目以及答案1
1.现有如下定义的字符串: NSMutableString * str=@“iphoneAndroid”,能不能对该字符串进行修改,如果能,请输出删除Android后的新字符串。 2求字符串“158”和“39”按十进制数值做差后的结果以字符串形式输出 3取出符串“123-456-789-000”中的数字部分,组成一个新的字符串输出,(提示:可变字符串;返回数组) 4放四个十进制三位数到一个数组中,然后按从小到大排序后组成一个新的数组
答案:
#import "KAppDelegate.h"
@implementation KAppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]] autorelease];
//1
NSMutableString *str = [[NSMutableStringalloc] initWithString:@"iphoneAndroid"];
NSRange r1 = {6,7};
NSRange r2 = NSMakeRange(6, 7);
[str deleteCharactersInRange:r2];
NSLog(@"str = %@",str);
[str release];
//2
int a = 158;
int b = 39;
int c = a - b;
NSString *str1 = [NSString stringWithFormat:@"%d",c];
NSLog(@"str1 = %@",str1);
//3
NSString *str2 = @"123-456-789-000";
NSArray *arr = [str2 componentsSeparatedByString:@"-"];
NSLog(@"arr = %@",arr);
NSString *str3=[arr componentsJoinedByString:@""];
NSLog(@"str3 = %@",str3);
//4
NSMutableArray *arr2 = [NSMutableArrayarrayWithObjects:@"901",@"675",@"110",@"123",nil];
//NSArray *ar = @[@"123",@"345"];//只支持6.0以上的系统
for (int i = 0; i<arr2.count - 1; i++)
{
for (int j = 0; j < arr2.count - i - 1; j++)
{
NSString *p = [arr2 objectAtIndex:j];
NSString *q = [arr2 objectAtIndex:j+1];
if ([p intValue] > [q intValue])
{
//[arr2 exchangeObjectAtIndex:j+1 withObjectAtIndex:j];//只支持6.0以上的系统
NSString *tmp = arr2[j];
arr2[j] = arr2[j+1];
arr2[j+1] = tmp;
}
}
}
NSLog(@"arr2 = %@",arr2);
self.window.backgroundColor = [UIColorwhiteColor];
[self.windowmakeKeyAndVisible];
returnYES;
}
浙公网安备 33010602011771号