试着玩Swift String。我想将String中的所有字符替换为空格。
inputString = "This is a String"
outputString = " "
我怎么能在swift中实现这个目标?
答案 0 :(得分:4)
一种可能的方式:
outputString = String(count: inputString.characters.count, repeatedValue: (" " as Character))
答案 1 :(得分:2)
您可以使用map
功能将任何字符替换为另一个字符:
String("foo".characters.map { _ in Character(" ") })
答案 2 :(得分:2)
另一种方式:
let outputString = inputString.stringByReplacingOccurrencesOfString("[^\\s]",
withString: " ",
options: .RegularExpressionSearch,
range: Range(inputString.startIndex..<inputString.endIndex))
答案 3 :(得分:1)
所以给定String
个n
个字符的输入String
,您需要另外n
个let inputString = "This is a String"
let outputString = String([Character](count: inputString.characters.count, repeatedValue: " "))
个空格,对吗?
Const FOF_CREATEPROGRESSDLG = &H0&
strTargetFolder = "\\computer\target"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(strTargetFolder)
objFolder.CopyHere "C:\tmp\copy\*.*", FOF_CREATEPROGRESSDLG