加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

资本化第一句话(斯威夫特)

发布时间:2020-12-14 05:22:19 所属栏目:百科 来源:网络整理
导读:我有一个字符串描述来保存我的句子.我只想把第一个字母大写.我尝试了不同的东西,但大多数都给了我例外和错误.我正在使用 Xcode 6. 这是我到目前为止所尝试的 let cap = [description.substringToIndex(advance(0,1))] as String description = cap.uppercase
我有一个字符串描述来保存我的句子.我只想把第一个字母大写.我尝试了不同的东西,但大多数都给了我例外和错误.我正在使用 Xcode 6.

这是我到目前为止所尝试的

let cap = [description.substringToIndex(advance(0,1))] as String
    description = cap.uppercaseString + description.substringFromIndex(1)

它给了我:类型’String.Index’不符合协议’IntegerLiteralConvertible’

我试过了

func capitalizedStringWithLocale(locale:0) -> String

但我无法弄清楚如何让它发挥作用.
有任何想法吗?

import Foundation

// A lowercase string
let description = "the quick brown fox jumps over the lazy dog."

// The start index is the first letter
let first = description.startIndex

// The rest of the string goes from the position after the first letter
// to the end.
let rest = advance(first,1)..<description.endIndex

// Glue these two ranges together,with the first uppercased,and you'll
// get the result you want. Note that I'm using description[first...first]
// to get the first letter because I want a String,not a Character,which
// is what you'd get with description[first].
let capitalised = description[first...first].uppercaseString + description[rest]

// Result: "The quick brown fox jumps over the lazy dog."

您可能希望确保在开始之前句子中至少有一个字符,否则您将在尝试将索引超出字符串末尾时遇到运行时错误.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读