假设我有一个包含三个下划线的字符串
var myString = "___"
给定一个数字,字符串应该如下所示
myString = "1__"
如果用户键入新号码,例如2,则字符串将更改
myString = "12_"
鉴于number
,我尝试编码这种情况:
for i in myString.characters {
if (i == "_") {
newString = myString.stringByReplacingOccurrencesOfString("_", withString: number, options: NSStringCompareOptions.LiteralSearch, range: nil)
break
}
}
问题在于stringByReplacingOccurrencesOfString
一次替换所有字符。您认为我应该使用哪种字符串方法?
答案 0 :(得分:3)
您可以使用用户输入字符串并使用“_”填充它:
var userInput = "1" // This is the string from the text field input by the user
var finalText = userInput.stringByPaddingToLength(3, withString: "_", startingAtIndex: 0)
答案 1 :(得分:3)
我亲自投票给@giorashc:s native padding solution(*),但另外要添加各种答案这个问题(因为此Q& A已包含一些变体),您也可以使用用于模板字符串suffixFrom(...)
的{{1}}方法(CharacterView
)来实现填充行为。 E.g:
___
(*)感谢@KennethBruno指出/* Example (if length of string >= template: just return string) */
let padToTemplate: (str: String, withTemplate: String) -> String = {
return $0.characters.count < $1.characters.count
? $0 + String($1.characters.suffixFrom($0.characters.endIndex))
: $0
}
/* Example usage */
let myString = padToTemplate(str: "1", withTemplate: "___")
print(myString) // 1__
来自.stringByPaddingToLength(..)
而不是原生的Swift!
答案 2 :(得分:2)
您可以在指定要替换的范围后使用stringByReplacingCharactersInRange
,如下所示
let range = myString.rangeOfString("_")
if range != nil {
myString.stringByReplacingCharactersInRange(range, withString:number)
}
答案 3 :(得分:2)
您可以使用简单搜索并替换:
var template = "_____"
func replace(template: String, _ character: String) -> String {
var newString = template
if let index = template.rangeOfString("_") {
newString.replaceRange(index, with: character)
}
return newString
}
template = replace(template, "1")
print(template)
template = replace(template, "2")
print(template)
请注意,有更好的解决方案。就个人而言,我会将值保存为数组,我只会在需要时动态构建字符串,因为使用字符数组会更容易。
答案 4 :(得分:1)
我确信有更优雅的解决方案,但也许有帮助:
var myString = "___"
myString = "1__"
let input = "6"
for index in 0 ..< myString.characters.count {
let startIndex = myString.startIndex.advancedBy(index)
let endIndex = startIndex.advancedBy(1)
let range = startIndex..<endIndex
let substring = myString[range]
if substring == "_" {
myString = myString.stringByReplacingCharactersInRange(range, withString: input)
break
}
}
答案 5 :(得分:1)
只需一种方法,使用计算属性和普通Swift,不使用NSString方法。
class Display {
var accumulated = [String]()
var display:String {
return (accumulated.prefix(3) + ["_","_","_"])
.prefix(3).joinWithSeparator("")
}
func addNumber(number:Int) {
accumulated.append(String(number))
}
}
let first = Display()
print(first.display) // => "___"
first.addNumber(1)
print(first.display) // => "1__"
first.addNumber(2)
print(first.display) // => "12_"
first.addNumber(3)
print(first.display) // => "123"
first.addNumber(4)
print(first.display) // => "123"
答案 6 :(得分:1)
'纯'快速解决方案(无需基金会)
var myString = "___"
let input = ["1","2","3","4","5"]
var arr = myString.characters.map { String($0) }
var idx = 0
input.forEach { (i) in
if idx < myString.characters.count {
arr.insert(i, atIndex: arr.startIndex.advancedBy(idx))
idx += 1
arr.removeLast()
myString = arr.joinWithSeparator("")
print(myString)
}
}
/*
1__
12_
123
*/
答案 7 :(得分:-1)
乍一看你的问题,你应该使用范围选项/参数,如:
if let range = myString.rangeOfString("_") {
myString = myString.stringByReplacingOccurrencesOfString("_", withString: number, options: NSStringCompareOptions.LiteralSearch, range: range)
}