如何直接将数组中的所有字符串大写? [英] How can I capitalize all the strings inside an array directly?

查看:405
本文介绍了如何直接将数组中的所有字符串大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我快速学习。我一直在Playground尝试这个。我不知道为什么字符串没有在这里大写。或者有没有其他方法可以直接大写数组中的字符串?



这是我的代码。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆