替换Swift中的许多字符串



我想替换字符串中的许多字符,这样看起来键盘移动了一点。

这是程序执行的内容:*qwertyuiopasdfghjklzxcvbnmmmmmmmmmmm*

为什么程序总是优先考虑最后一个?

有人能告诉我为什么这是错误的,以及如何修复它吗

import UIKit
var str = "qwertyuiopasdfghjklzxcvbnm"
var replaced = str.replacingOccurrences(of: "m", with: "n").replacingOccurrences(of: "n", with: "b")    .replacingOccurrences(of: "b", with: "v").replacingOccurrences(of: "v", with: "c")    .replacingOccurrences(of: "c", with: "x").replacingOccurrences(of: "x", with: "z")    .replacingOccurrences(of: "z", with: "l").replacingOccurrences(of: "l", with: "k")    .replacingOccurrences(of: "k", with: "j").replacingOccurrences(of: "j", with: "h")    .replacingOccurrences(of: "h", with: "g").replacingOccurrences(of: "g", with: "g")    .replacingOccurrences(of: "g", with: "f").replacingOccurrences(of: "f", with: "d")    .replacingOccurrences(of: "d", with: "s").replacingOccurrences(of: "s", with: "a")    .replacingOccurrences(of: "a", with: "p").replacingOccurrences(of: "p", with: "o")    .replacingOccurrences(of: "o", with: "i").replacingOccurrences(of: "i", with: "u")    .replacingOccurrences(of: "u", with: "y").replacingOccurrences(of: "y", with: "t")    .replacingOccurrences(of: "t", with: "r").replacingOccurrences(of: "r", with: "e")    .replacingOccurrences(of: "e", with: "w").replacingOccurrences(of: "w", with: "q")    .replacingOccurrences(of: "q", with: "m")
print(str)
print(replaced)

正如其他人所说,问题是您一次重新映射一个字符,而且顺序不对,所以首先要用n替换所有的m。然后替换所有的n。但这包括最初的m。解决这个问题的一种方法是走另一条路。因此,用n替换m,然后用m替换q,依此类推

一个更好的方法是一次完成所有的替换。

首先创建一个函数,以您想要的方式转换字符

func keyboardShift(c: Character) -> Character
{
switch  c
{
case "q" : return "m"
case "w" : return "q"
case "e" : return "w"
case "r" : return "e"
case "t" : return "r"
case "y" : return "t"
case "u" : return "y"
case "i" : return "u"
case "o" : return "i"
case "p" : return "o"
case "a" : return "p"
case "s" : return "a"
case "d" : return "s"
case "f" : return "d"
case "g" : return "f"
case "h" : return "g"
case "j" : return "h"
case "k" : return "j"
case "l" : return "k"
case "z" : return "l"
case "x" : return "z"
case "c" : return "x"
case "v" : return "c"
case "b" : return "v"
case "n" : return "b"
case "m" : return "n"
default: return c
}
}

然后将其用作map()的参数。

let shiftArray = "the quick brown fox jumps over the lazy dog".map(keyboardShift)

map()返回Array<Character>,因此您需要从中创建一个新的String

shifted = String(shiftArray)
// shifted is 'rgw myuxj veiqb diz hynoa icwe rgw kplt sif'

您的问题是所有的replacingOccurrences(of: with:)都是按顺序运行的,结果是qw变成ww,变成eee,然后变成rrrr等等

我在操场上尝试过,所以这里是你的解决方案:

您需要做的是将字符串分解为一个字符数组。

你想把键盘1移到右边,所以取这个数组的最后一个字符,并用.append()把它粘在第一个元素中

在这里,这是有效的:(

var str = "qwertyuiopasdfghjklzxcvbnm"
var strArray = Array(str) //turn str into an array
var last = str.last //get the last character of the array
strArray.popLast() //remove the last element/character of the array
strArray.insert(last!, at: 0) //insert the last character of the array in. the first element and push everything along 1
var string = ""
for char in strArray {
string.append(char)
}
print(str) //input
print(strArray)//broken down string
print(string)//output

线路print(string)输出mqwertyuiopasdfghjklzxcvbn,我相信这就是你想要的:(

基于操场运行这段代码的速度,我建议这可能比依次替换字符串的每个字符更有效:p

最新更新