替换Swift中的许多字符串
我想替换字符串中的许多字符,这样看起来键盘移动了一点 这是程序执行的内容: *qwertyuiopasdfghjklzxcvbnm mmmmmmmmmmmmmmmmmmmmmmmm * 为什么程序总是优先考虑最后一个 有人能告诉我为什么这是错误的,以及如何修复它吗替换Swift中的许多字符串,swift,string,Swift,String,我想替换字符串中的许多字符,这样看起来键盘移动了一点 这是程序执行的内容: *qwertyuiopasdfghjklzxcvbnm mmmmmmmmmmmmmmmmmmmmmmmm * 为什么程序总是优先考虑最后一个 有人能告诉我为什么这是错误的,以及如何修复它吗 import UIKit var str = "qwertyuiopasdfghjklzxcvbnm" var replaced = str.replacingOccurrences(of: "m&qu
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)
您的问题是,所有的
replacingOccurrences(of:with:)
都是按顺序运行的,结果是qw变成ww,然后变成eee,然后变成rrrrr等等
我在操场上尝试了一下,下面是你的解决方案:
您需要做的是将字符串拆分为一个字符数组
您希望将键盘1向右移动,因此使用.append()
在这里,这是有效的:)
行print(string)
输出mqwertyuiopasdfghjklzxcvn
,我相信这就是您想要的:)
基于Playerd运行这段代码的速度,我认为这可能比依次替换字符串的每个字符更有效:p您的问题是所有的
重复发生(of:with:)
都是按顺序运行的,结果是qw变成ww,变成eee,然后变成rrrr等等
我在操场上尝试了一下,下面是你的解决方案:
您需要做的是将字符串拆分为一个字符数组
您希望将键盘1向右移动,因此使用.append()
在这里,这是有效的:)
行print(string)
输出mqwertyuiopasdfghjklzxcvn
,我相信这就是您想要的:)
基于Playerd运行这段代码的速度,我认为这可能比依次替换字符串中的每个字符更有效:p正如其他人所说,问题是您一次只重新映射一个字符,而且顺序错误,因此,首先将所有的
m
s替换为n
。然后替换所有的n
s。但这包括最初的m
。解决这个问题的一个办法是走另一条路。因此,将m
替换为n
,然后将q
替换为m
,依此类推
更好的方法是一次完成所有替换
首先创建一个函数,以按照您想要的方式转换字符
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()
的参数
map()
shifted = String(shiftArray)
// shifted is 'rgw myuxj veiqb diz hynoa icwe rgw kplt sif'
正如其他人所说的,问题是您一次只重新映射一个字符,而且您的顺序不正确,因此首先您要将所有的m
s替换为n
。然后替换所有的n
s。但这包括最初的m
。解决这个问题的一个办法是走另一条路。因此,将m
替换为n
,然后将q
替换为m
,依此类推
更好的方法是一次完成所有替换
首先创建一个函数,以按照您想要的方式转换字符
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()
的参数
map()
shifted = String(shiftArray)
// shifted is 'rgw myuxj veiqb diz hynoa icwe rgw kplt sif'
别那样做。取而代之的是:var replaced=str.replacingOccurrences(of:“m”,用“n”);打印(替换);替换=替换。替换发生(由“n”替换为“b”);打印(替换);替换=…
并调试。您可能能够看到发生了什么。您希望它输出什么?var str=“mnbvcxz”var replaced=str.replacingOccurrences(of:“m”,with:“n”);打印(替换);替换=替换。replacingOccurrences(of:“n”,with:“b”);打印(替换);替换=替换。replacingOccurrences(of::替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的,替换的;打印(替换)帮助我按照你告诉我的做了,但现在它执行:nnbvvcxz bbbvvvvvvcxz ccccc xz xxxxxx z zzzzzzz mmmmmmmmmmm mnbvcxznzvcxm-这是我想打印的内容不要那样做。相反:var replaced=str.replacingOccurrences(of:“m”,with:“n”);打印(替换);替换=替换。replacingOccurrences(of:“n”,with::打印(替换);替换=…
并调试。您可能能够看到发生了什么。您希望它输出什么?var str=“mnbvcxz”var replacement=str.replacingOccurrences(of:“m”,with:“n”);打印(替换);替换=替换。replacingOccurrences(of:“n”,with:“b”);打印(替换);替换=替换。replacingOccurrences(of:“v”,with:“c”);print(replacement);replaccingoccurrences(of:“v”,with:“c”);print(replacement);replaccingoccurrences(of:“c”,with:“x”);print(replacement);replaccingoccurrences(of:“x”,with:“z”);print(replacement);replacement=replaccingoccurrences(of:“z”,with::“m”);打印(替换)帮助我按照你告诉我的做了,但现在执行:nnbvcxz bbbvcxz vvvvvcxz ccccc xz xxxxxx z zzzzzzz mmmmmmmmmmm mnbvcxznzvcxm-这是我想打印的内容我写的qwerty位(在顶部)是错误的,但重点仍然是…只是顺序相反哈哈:)我写的qwerty位(在顶部)走错了方向,但重点仍然在……只是顺序相反哈哈:)