在C#.NET中解析文本(一个小问题) [英] Parsing text in C#.NET (a little problem)

查看:101
本文介绍了在C#.NET中解析文本(一个小问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  string  input = textbox1.Text; 
Dictionary< string,string> map = new 字典< string,string>();

map.Add( AB x);
map.Add( Ab z);
map.Add( aB p);

string temp = input;
foreach var 条目 map)
{
string key = entry.Key;
string value = entry.Value;
temp = Regex.Replace(temp,key,match = >
{
bool isUpper = char .IsUpper(match.Value [ 0 ]);

char [] result = value .ToCharArray();
结果[ 0 ] = isUpper
char .ToUpper(result [ 0 ])
char .ToLower(结果[ 0 ]);
return new string (result);
},RegexOptions.IgnoreCase);

}





这是我的代码......我用它来解析我的小词。像AB = x CB = z等等...



但是我的问题是 - 它无法理解lowecase和upercase(不具备情感性)



看我映射:



map.Add(AB,x);

map.Add(ab,z);

map.Add(aB,p);



我为每个AB,Ab,aB映射了不同的结果。



但是当我把AB,Ab,aB它只显示x时!



但是我希望显示不同的结果。



我的编码问题在哪里。



我尝试了什么:



map.Add(AB,x );

map.Add(ab,z);

map.Add(aB,p);

解决方案

你可以使用类似的东西:

 Dictionary< string,string> map =  new 字典< string,string>(
StringComparer.Ordinal);



请参阅: Dictionary(TKey,TValue)构造函数(IEqualityComparer(TKey))(System.Collections .Generic) [ ^ ]


string input = textbox1.Text;
Dictionary<string, string> map = new Dictionary<string, string>();

map.Add("AB", "x");
map.Add("Ab", "z");
map.Add("aB", "p");

string temp = input;
foreach (var entry in map)
{
    string key = entry.Key;
    string value = entry.Value;
    temp = Regex.Replace(temp, key, match =>
    {
        bool isUpper = char.IsUpper(match.Value[0]);

        char[] result = value.ToCharArray();
        result[0] = isUpper
            ? char.ToUpper(result[0])
            : char.ToLower(result[0]);
        return new string(result);
    }, RegexOptions.IgnoreCase);

}



This is my code...I use this for parse my small words. Like AB=x CB=z etc etc...

But my problem is - it can't understand lowecase and upercase (not case sensative)

Look I mapped :

map.Add("AB", "x");
map.Add("Ab", "z");
map.Add("aB", "p");

I mapped every AB,Ab,aB for different result.

But when i put AB,Ab,aB it just shows "x" !

But i want to show different result for eaches.

Where is my coding problem.

What I have tried:

map.Add("AB", "x");
map.Add("Ab", "z");
map.Add("aB", "p");

解决方案

You can use something like:

Dictionary<string, string> map = new Dictionary<string, string>( 
                          StringComparer.Ordinal);


See: Dictionary(TKey, TValue) Constructor (IEqualityComparer(TKey)) (System.Collections.Generic)[^]


这篇关于在C#.NET中解析文本(一个小问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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