如何轻松解析字符串? [英] How to parse string easily?

查看:91
本文介绍了如何轻松解析字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本框 -

Textbox1和文本框1



我想解析文本 -



double =你好

code =怎么

享乐=是

今天=你



如果我在textbox1中键入double,它会在文本框2中显示hello。



假设我键入texbox1 -

--------------------------

今天双码享受


textbox2中的
将显示 -

--------------------------

你好,你怎么样?

解决方案

我会使用Dictionary< string,>并且要替换的词是关键,要替换的词是值。循环遍历您的字典,并使用RegEx或Replace替换每个值。你必须做些什么来确保案例保持不变,如果你谷歌c#find replace keep case你可能会找到示例代码。


check

更换单词c#中字典值的字符串 [ ^ ]

将一组单词替换为另一个单词 [ ^ ]


也许不是最好的,但我会这样做:



设置字典< string,string>为了你所有的话。关键是双,值是你好等。



每次都完整地解析文本框的值:



字典<字符串,字符串> cypher =  new 字典< string,string> 
{
{ double hello},
/// etc
};

string 解码( string 代码){
返回代码
.split(' ' // 将短语拆分为单词
。选择(c = >
cypher.ContainsKey(c) // 检查工作在词典中
?cypher [c] // 返回解码后的单词
:c); // 返回代码字(或其他)
}







不知道你为什么要这个。有更好的密码使用完整的单词甚至句子。虽然



希望有所帮助^ _ ^



Andy


I have two textbox -
Textbox1 and textbox 1

I want to parse text -

double = hello
code=how
enjoy=are
today=you

If I type "double" in textbox1 it will show "hello" in textbox two.

suppose I type texbox1 -
--------------------------
double code enjoy today

in textbox2 will show -
--------------------------
hello how are you

解决方案

I'd use a Dictionary<string,> and the word to replace is the key and the word to replace with is the value. Loop through your dictionary and replace each key with each value using RegEx or Replace. You'll have to do something to ensure the case remains the same though, if you google "c# find replace keep case" you'll probably find sample code.


check
Replacing words in a string with values from Dictionary in c#[^]
Replacing set of words with another[^]


Maybe not the best, but this is how I would do it:

Set up a Dictionary<string,string> for all of your words. The key is "double", the value is "hello" etc.

parse the value of the textbox in it's entirety each time like so:

Dictionary<string,string> cypher = new Dictionary<string,string>
{
  {"double","hello"},
  ///etc
};

string Decode(string code){
    return code
            .split(' ')  //Split the phrase into words
            .Select(c=> 
                cypher.ContainsKey(c)  //Check the work is in the dictionary
                   ?cypher[c]          //Return the decoded word
                   :c);                //return the code word (or whatever)
}




No idea why you want this. There are much better cyphers out there that use full words and even sentences. Granted they are more complex to write codes for, though

Hope that helps ^_^

Andy


这篇关于如何轻松解析字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆