字符串分割到数组,数组在转换成字符串
//
// main.m
//
// Created by lishabai on 16/1/3.
// Copyright (c) 2016年 lishabai. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *str = @"hello! you are a good ,boy!";
// 以@“ ”字符串分割str字符串以字符串的类型放到NSArray的不可变数组中
NSArray *array = [str componentsSeparatedByString:@" "];
// 以@“ ,!”这个字符串中的每一个字符分割str字符串放到NSArray的不可变字符串中
NSArray *array1 = [str componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" ,!"]];
// 把array的数组转换成一个以@“ ”分割的字符串!
NSString *str1 = [array componentsJoinedByString:@" "];
NSLog(@"%@",str1);
// NSArray 和 NSMutableArray的数组都可以
NSMutableArray *array2 = [NSMutableArray arrayWithArray:array];
NSString *str2 = [array2 componentsJoinedByString:@"="];
NSLog(@"%@",str2);
// 遍历字数组取出字符串!
for (NSString *temp in array1) {
NSLog(@"%@",temp);
}
}
return 0;
}