//
// main.m
// 字符串重组
//
// Created by admin on 15/8/13.
// Copyright (c) 2015年 admin. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc,
const char * argv[]) {
@autoreleasepool {
// 1. 多个数据拼接成一个字符串
int year = 2015;
int month = 8;
int day = 10;
NSString *date = [NSString
stringWithFormat:@"%d年%d月%d日",year,month,day];
NSLog(@"日期为 :%@",date);
NSString *address =
@"地球的某一个地方";
NSString *dateAndAddress = [NSString
stringWithFormat:@"时间:%@
地点:%@",date,address];
NSLog(@"%@",dateAndAddress);
// 2. 在字符串的末尾追加新的字符
NSString *str1 = @"www.";
NSString *str2 = [str1
stringByAppendingString:@"baidu"];
NSString *str3 = [str2
stringByAppendingString:@".com"];
NSLog(@"str1: %@ str2:%@ str3:%@", str1, str2, str3);
// 3. 在自定义的范围内追加字符串
NSString *str4 = @"www..com";
NSRange range = {4,5};
NSString *str5 = [str3
stringByReplacingCharactersInRange:range
withString:@"baidu"];
NSLog(@" str4: %@ str5: %@", str4, str5);
// 4. 使用新的字符串,替换原有的字符
NSString *str6 = @"www.baidu.com";
// (自定义替换的位置)
NSString *str7 = [str6
stringByReplacingCharactersInRange:range
withString:@"google"];
NSLog(@"str6: %@ str7: %@", str6, str7);
// (将字符串中的 w
全部替换成 t)
NSString *str8 = [str6
stringByReplacingOccurrencesOfString:@"w"
withString:@"t"];
NSLog(@"str8: %@", str8);
// 5. 在字符串中删除某些特定的字符
既:将上面的t换成空
NSString *str9 = [str6
stringByReplacingOccurrencesOfString:@"."
withString:@""];
NSLog(@"str9: %@", str9);
// 6. 去掉字符串中的空格
NSString *str10 =
@"www. bai du.co m";
NSString *str11 = [str10
stringByReplacingOccurrencesOfString:@" "
withString:@""];
NSLog(@"str10: %@ str11: %@", str10, str11);
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。