如何直接将数组中的所有字符串大写? [英] How can I capitalize all the strings inside an array directly?
本文介绍了如何直接将数组中的所有字符串大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码。
var dogNames = [Sean,fido,Sarah,Parker,Walt,abby,Yang]
for index in 0 ..< dogNames.count {
var dogName = dogNames [index] .capitalizedString
dogNames.removeAtIndex(index)
dogNames.append(dogName)
}
当我尝试再次显示变量dogNames时。里面的字符串没有大写。
解决方案
也回答我自己的问题。总结我在答案中找到的所有内容。我想出了这个解决方案。这就是我用较少的过程来解决这个问题。
var dogNames = [Sean,fido,Sarah ,Parker,Walt,abby,Yang]
指数为0 ..< dogNames.count {
if dogNames [index]!= dogNames [index] .capitalizedString {
var dogName = dogNames [index] .capitalizedString
dogNames [index] = dogName
}
}
I'm learning swift. I've been trying this in Playground. I have no idea why the string is not being capitalized here. Or is there any other way to capitalize the string inside the array directly?
Here's my code.
var dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]
for index in 0..<dogNames.count {
var dogName = dogNames[index].capitalizedString
dogNames.removeAtIndex(index)
dogNames.append(dogName)
}
When I try to display again the variable dogNames. The strings inside are not being capitalized.
解决方案
To answer my own question as well. Summarizing everything I found in the answers here. I came up with this solution. This is what I did to fix this with less process.
var dogNames = ["Sean", "fido", "Sarah", "Parker", "Walt", "abby", "Yang"]
for index in 0..<dogNames.count {
if dogNames[index] != dogNames[index].capitalizedString {
var dogName = dogNames[index].capitalizedString
dogNames[index] = dogName
}
}
这篇关于如何直接将数组中的所有字符串大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文