将字符串拆分为字母

时间:2015-10-17 14:01:28

标签: ios arrays string swift2 xcode7

假设我有String - "Hello" 如何将String拆分为Array,将每个character分成["H", "e", "l", "l", "o"] - public async Task<ActionResult> Home(RenderModel model) { var menuModel = new HomeViewModel(model); await Task.Delay(1000); return View("Home", menuModel); } 感谢。

2 个答案:

答案 0 :(得分:8)

字符串中的字符放在String集合下。只需迭代它们并将它们转换为let str = "Hello" let arr = str.map { String($0) } print(arr)

Swift 4

let str =  "Hello"
let arr = str.characters.map { String($0) }
print(arr)

Swift 3

Function myaverage(x As Double, y As Double, z As Double) As Double
    myaverage = 0.4 * x + 0.5 * y + 0.1 * z
End Function

答案 1 :(得分:-4)

我认为你必须手动做一些。

NSString *str = @"Hello";
NSMutableArray *chars = [NSMutableArray new];
for (NSUInteger i = 0; i < [str length]; i++) {
    unichar c = [str characterAtIndex:i];
    [chars addObject:[NSString stringWithCharacters:&c length:1]]; 
}