替换Swift中String中的所有字符

时间:2016-06-21 09:56:03

标签: swift string

试着玩Swift String。我想将String中的所有字符替换为空格。

inputString = "This is a String"

outputString = "                "

我怎么能在swift中实现这个目标?

4 个答案:

答案 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)

所以给定Stringn个字符的输入String,您需要另外nlet 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