gpt4 book ai didi

swift - stringByReplacingOccurrencesOfString 删除整个字符串

转载 作者:行者123 更新时间:2023-11-30 10:07:36 26 4
gpt4 key购买 nike

我正在尝试将字符串写入文本框,但如果第一个字符是 * 或字符串 == ""我不想写入该字符串,但我想删除所有要做的空格这个

for command in InitialCommandsArr {
let nulledCommand = command.stringByReplacingOccurrencesOfString(" ", withString: "")

if nulledCommand != "" &&
command.stringByReplacingOccurrencesOfString(" ", withString: "").characters.first != "*"
{
OutputCommand.text = "\(OutputCommand.text),{id:\"MinecartCommandBlock\",Command:\(command)}}"
}
}

当我这样做时,它用“”帮助替换了整个字符串?

最佳答案

刚刚尝试过这个:

    var labelText = "";
let initialCommandsArr = ["foo","bar","*baz"," "]

for command in initialCommandsArr {
let nulledCommand = command.stringByReplacingOccurrencesOfString(" ", withString: "")
if(nulledCommand.characters.count > 0 && nulledCommand.characters.first != "*"){

labelText += ",{id:\"MinecartCommandBlock\",Command: \(command) }}"
}
}

print(labelText)

打印出:

,{id:"MinecartCommandBlock",Command: foo }},{id:"MinecartCommandBlock",Command: bar }}

那是你想要的吗?

关于swift - stringByReplacingOccurrencesOfString 删除整个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35183381/

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