2010-10-07 20:51:35| 分类: objective c | 标签: |举报 |字号大中小 订阅
下全是代码,你可以直接复制测试;本人也是新手学习中,错误请大家指正,谢谢!
//输出格式化字符串;
NSString *heigt;
heigt = [NSString stringWithFormat:@"you height is %d feet %d inches",4,9];
NSLog(@”%@”,heigt);
//you height is 4 feet 9 inches;
//声名两个字符;
NSString *thing1 = @”book”;
NSString *thing2 = @”Book”;
//比较两个字符是否一样;区分大小写的;//这里的判断用了用了一个三元简写;后面就不再说了;
[thing1 isEqualToString:thing2]?NSLog(@”they are the same!”):NSLog(@”not the same!”);
//not the same!;
//不区分大小写NSCaseInsensitiveSearch, 字符长度对比NSNumericSearch;
[thing1 compare:thing2 options:NSCaseInsensitiveSearch | NSNumericSearch]==NSOrderedSame?NSLog(@”same”):NSLog(@”not the Same”);
//same
NSString *fileName = @”test.move”;
//是否以”test”开头;
if ([fileName hasPrefix:@"test"]) {
NSLog(@”has Test String !”);
}else{
NSLog(@”don’t have Test”);
}
//has Test String !
//是否以”.move”结尾;
[fileName hasSuffix:@".move"]?NSLog(@”Yes it got a .Mov in its end”):NSLog(@”no it has no .mov string”);
//Yes it got a .Mov in its end
//声名一个可变字符;长度是40个字符;
NSMutableString *myMutableString;
myMutableString = [NSMutableString stringWithCapacity:40];
NSString *myName = @”Leo”;
[myMutableString appendString:@"Hello ,there"];
[myMutableString appendFormat:@" i am %@",myName];
NSLog(@”this is NSMutableString: %@”,myMutableString);
//this is NSMutableString: Hello ,there i am Leo;
//修改可变字符;先声名一个可变字符 myFriend;长度30;
NSMutableString *myGirlFriend;
myGirlFriend = [NSMutableString stringWithCapacity:30];
//然后给字符加入一些内容;
[myGirlFriend appendString:@"Here are my GF:Carol Sophia Ashley Helen and Yoyo"];
//输出一下;
NSLog(@”%@”,myGirlFriend);
//Here are my GF:Carol Sophia Ashley Helen and Yoyo;
//声名一个变动范围(NSRange);PS:先这么叫吧,不知道叫什么好!大家指点下;
NSRange joneRange;
joneRange = [myGirlFriend rangeOfString:@"Helen "];
//看到书上例子这里是长度自增一下,为了把后面的空格带上,我上面直接加了个空格测试也可以,偷懒一下;
//joneRange.length++;
//下面:就是从myFriend字符中配对,如果有相等的内容就删除了;这个应该容易看懂;
[myGirlFriend deleteCharactersInRange:joneRange];
//再输出一下得到什么?;
NSLog(@”%@”,myGirlFriend);
//Here are my GF:Carol Sophia Ashley and Yoyo;
//Helen not my GF anyMore,why? We broke up;^_^
//可变数组;
NSNumber *number;
//声明可变数组,并要用 arrayWithCapacity 初始设置数组的长度;
NSMutableArray *array;
array = [NSMutableArray arrayWithCapacity:2];
number = [NSNumber numberWithInt:40];
//然后往数组里加入:number 和字符对象 “test”;
[array addObject:number];
[array addObject:@"test"];
//循环输出一下;
for(int i = 0;i<[array count];i++){
NSLog(@”%@”,[array objectAtIndex:i]);
}
//40;
//test;
评论