替换字符串中的字符列表,并且不覆盖替换的字符
我正在为我的一个爱好而开发这个解密程序:地理藏宝。
我的想法
是有一个基本的字符列表:
[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9]
在 Windows 窗体中,您可以指定每个字符应该替换为哪个字符。
[A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,9,8,7,6,5,4,3,2,1,0]
所以这会导致我的要解密的字符串被转换为大写。
例如:the Quick Brown Fox Jumps Over the LaZY DOG
会导致 THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
我如何解决这个问题
public static string DecodeWithKey(string source, string keyBase, string keyValue)
{
StringBuilder sb = new StringBuilder(source);
for (int i = 0; i < keyBase.Length; i++)
{
sb = sb.Replace(keyBase[i], keyValue[i]);
}
return sb.ToString();
}
现在这对于上面的示例来说效果很好。但当你开始移动角色时,它就完全失败了。
问题出在事情的顺序上
,所以让我们说这又是我的基础:
[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9]
现在我使用更实际的值来替换:
[a,b,c,d,t,f,g,h,i,j,k,l,m,n,o,p,q,r,s,p,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9]
注意,我用 t 更改了 e,用 p 更改了 t。
结果:the Quick Brown Fox Jumps Over the Lazy Dog
变成 PHP Quick Brown Fox Jumps ovpr php Lazy Dog
正如您在 中看到的那样将其替换为
tht
,然后替换为 php
。从我的逻辑来看,这是显而易见的。
我怎样才能防止这种情况发生?这样就显示了 pht
并且不会替换一个字符两次。
我希望我已经解释清楚了。
I'm working on this decryption program for a hobby of mine: geocaching.
The idea
I have a base list of chars :
[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9]
And in a windows form you can specify with which character each character should be replaced.
[A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,9,8,7,6,5,4,3,2,1,0]
So this would result in my string to decrypt being converted into uppercase.
example: the quick brown fox jumps over the lazy dog
would result in THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
How I solved it
public static string DecodeWithKey(string source, string keyBase, string keyValue)
{
StringBuilder sb = new StringBuilder(source);
for (int i = 0; i < keyBase.Length; i++)
{
sb = sb.Replace(keyBase[i], keyValue[i]);
}
return sb.ToString();
}
Now this works fine for the above sample. But it completely fails when you start moving characters around.
The problem is in the order of things
So let's say this is my base again:
[a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9]
And now I'm using a bit more realistic values to be replaced:
[a,b,c,d,t,f,g,h,i,j,k,l,m,n,o,p,q,r,s,p,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9]
Notice, I changed e with t and t with p.
The result: the quick brown fox jumps over the lazy dog
becomes php quick brown fox jumps ovpr php lazy dog
As you can see in the
it replaced it to tht
and then to php
. Which is obvious from my logic.
How can I prevent this? so that is shows pht
and doesn't replace a character twice.
I hope I have explained it clearly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不简单地逐个字符地构建解码的字符串,而不是就地修改源的副本?
首先,构建一个字典,将每个编码字符与其解码值相匹配
作为对 Piotr 评论的回应,让我们尝试在游戏中添加一些 LINQ:
Why don't you simply build the decoded string character by character rather than modifying a copy of the source in-place?
First, build a dictionary that matches each coded char to its decoded value
In response to Piotr comment, let's try to throw some LINQ into the game:
您可以使用正则表达式替换来执行您想要的操作。看看这个问题。
将替换内容转换为字典,然后:
You can use regex replace to do what you want. Have a look at this question.
Convert your replacements to a dictionary, and then: