gpt4 book ai didi

string - Swift 3 字符串插入

转载 作者:行者123 更新时间:2023-12-02 05:40:49 27 4
gpt4 key购买 nike

我有一个标签,其中的文本会根据某些上下文发生变化,我需要将数字“9”作为字符串插入到标签字符串中。假设我有“-102”,我想将“9”插入到字符串中,结果就像这样“-9102”,谁能告诉我如何编码。这 label.insert("9", ind:2)不起作用。

最佳答案

label.insert("9", ind:2)

对于索引,您需要提供索引,而不是整数。

 // to insert at 0 position
let str = label.text!;
label.text!.insert("9", at:str.startIndex);

// to insert at 2nd position
label.text!.insert("9", at: str.index(str.startIndex, offsetBy: 2)) ;

关于string - Swift 3 字符串插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40388507/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com