Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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 - Fatal编程技术网

Swift 如何用下划线替换字符串中的空格

Swift 如何用下划线替换字符串中的空格,swift,Swift,好的,我将天气数据拉入我的应用程序,为了拉入基于位置的api url,我必须将城市和简写状态(即CA、MO、NY等)插入api url。我可以得到城市和速记状态,问题是城市中的任何空间都需要格式化为下划线,例如纽约必须与纽约相等。我该怎么做呢?城市名称采用字符串格式。我已经看到了关于堆栈溢出的答案,但没有一个是Swift 4。正确的实现与Swift 3完全相同: let str = "New York" let replaced = str.replacingOccurrences(of: "

好的,我将天气数据拉入我的应用程序,为了拉入基于位置的api url,我必须将城市和简写状态(即CA、MO、NY等)插入api url。我可以得到城市和速记状态,问题是城市中的任何空间都需要格式化为下划线,例如纽约必须与纽约相等。我该怎么做呢?城市名称采用字符串格式。我已经看到了关于堆栈溢出的答案,但没有一个是Swift 4。

正确的实现与Swift 3完全相同:

let str = "New York"
let replaced = str.replacingOccurrences(of: " ", with: "_")

正确实施与Swift 3完全相同:

let str = "New York"
let replaced = str.replacingOccurrences(of: " ", with: "_")

如上文所述,问题很可能是重复的,通常最好只指向有答案的问题,而不是再次回答。如上文所述,问题很可能是重复的,通常最好只指向有答案的问题,而不是再次回答。