Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换Swift中字符串中的所有字符_Swift_String - Fatal编程技术网

替换Swift中字符串中的所有字符

替换Swift中字符串中的所有字符,swift,string,Swift,String,只是想用快捷的琴弦演奏一下。我想将字符串中的所有字符替换为空白 inputString = "This is a String" outputString = " " 如何通过swift实现这一点?一种可能的方法: outputString = String(count: inputString.characters.count, repeatedValue: (" " as Character)) 您可以使用map功能将任何字符替换为另一个字符: Strin

只是想用快捷的琴弦演奏一下。我想将字符串中的所有字符替换为空白

inputString = "This is a String"

outputString = "                "
如何通过swift实现这一点?

一种可能的方法:

outputString = String(count: inputString.characters.count, repeatedValue: (" " as Character))

您可以使用
map
功能将任何字符替换为另一个字符:

String("foo".characters.map { _ in Character(" ") })
另一种方式:

let outputString = inputString.replacingOccurrences(of: "[^\\s]",
                          with: " ", 
                       options: .regularExpression, 
                         range: inputString.startIndex..<inputString.endIndex)
let outputString=inputString.replacingOccurrences(of:“[^\\s]”,
有:“,
选项:。正则表达式,

范围:inputString.startIndex..因此,给定一个
String
n
字符的输入,您需要另一个
String
n
空格,对吗

let inputString = "This is a String"
let outputString = String([Character](count: inputString.characters.count, repeatedValue: " "))

可能与任何其他语言一样:计算原始字符串中的字符数,并生成一个由N个空格字符组成的新字符串…:)您应该将
length
替换为
count