替换字符串中的字符列表,并且不覆盖替换的字符

发布于 2024-12-04 20:09:04 字数 1440 浏览 1 评论 0原文

我正在为我的一个爱好而开发这个解密程序:地理藏宝。

我的想法

是有一个基本的字符列表:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月寒剑心 2024-12-11 20:09:04

为什么不简单地逐个字符地构建解码的字符串,而不是就地修改源的副本?

首先,构建一个字典,将每个编码字符与其解码值相匹配

public static string DecodeWithKey(string source, Dictionary<char,char> decoder) 
{ 
    StringBuilder sb = new StringBuilder(source.Length); 
    for (int i = 0; i < source.Length; i++) 
    { 
        sb.Append( decoder[source[i]] ); // error management left out as an exercice
    } 
    return sb.ToString(); 
}

作为对 Piotr 评论的回应,让我们尝试在游戏中添加一些 LINQ:

public static string DecodeWithKey(string source, Dictionary<char,char> decoder) 
{ 
    return String.Concat(from ch in source select decoder[ch]);
}

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

public static string DecodeWithKey(string source, Dictionary<char,char> decoder) 
{ 
    StringBuilder sb = new StringBuilder(source.Length); 
    for (int i = 0; i < source.Length; i++) 
    { 
        sb.Append( decoder[source[i]] ); // error management left out as an exercice
    } 
    return sb.ToString(); 
}

In response to Piotr comment, let's try to throw some LINQ into the game:

public static string DecodeWithKey(string source, Dictionary<char,char> decoder) 
{ 
    return String.Concat(from ch in source select decoder[ch]);
}
折戟 2024-12-11 20:09:04

您可以使用正则表达式替换来执行您想要的操作。看看这个问题

将替换内容转换为字典,然后:

var regex = new Regex(String.Join("|",map.Keys));
var newStr = regex.Replace(str, m => map[m.Value]);

You can use regex replace to do what you want. Have a look at this question.

Convert your replacements to a dictionary, and then:

var regex = new Regex(String.Join("|",map.Keys));
var newStr = regex.Replace(str, m => map[m.Value]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文