Mar*_*rco 1 c# linq generic-collections
我被困在一个检查字符串('14534000000875e')的函数中,如果它包含一个字母.如果它包含一个字母(az),请删除该字母并在末尾添加一个字符串.
为了实现这一点,我创建了一个Dictionary<char, string>Pairs,它已映射到09001,b到09002 [...]和z到09026
到目前为止这是我的代码:
public static string AlterSerial(string source)
{
Dictionary<char, string> pairs = new Dictionary<char, string>();
pairs.Add('a', "09001");
...
int index = source.IndexOf(x);
if (index != -1)
{
return source.Remove(index, 1);
}
return source;
}
Run Code Online (Sandbox Code Playgroud)
如何检查source字符串是否包含26个密钥中的一个,删除此密钥并将相应的字符串添加到源字符串的末尾?
注意:字母并不总是在源的末尾.
亲切的问候
试试这个:
Dictionary<char, string> pairs = new Dictionary<char, string>();
pairs.Add('a', "09001");
...
foreach(KeyValuePair<string, string> entry in pairs)
{
if (source.Contains(entry.key))
{
source = source.Replace(entry.key, "") + entry.Value;
break;
}
}
return source;
....
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6232 次 |
| 最近记录: |