我在此部分foreach( string code in text )
上出错,该错误表明无法将char转换为字符串。我如何将其转换为字符串
我的列表
class MyCipher : ICipherDecipher
{
private List<Code> alphabet;
public MyCipher()
{
alphabet = new List<Code>();
alphabet.Add(new Code("Aca", " 1234"));
alphabet.Add(new Code("Bb", " 1234"));
alphabet.Add(new Code("C1", " 1234"));
}
这是错误foreach
部分上的错误,它的说法不能从char转换为字符串
private string Cipher( string text )
{
StringBuilder result = new StringBuilder();
foreach( string code in text )
{
Code element =
alphabet.Where(x => x.MyCode == code.ToString()).SingleOrDefault();
if ( element != null)
{
result.Append(element.MyDecoded);
}
}
return result.ToString();
}
编辑后的代码
class MyCipher : ICipherDecipher
{
private List<Code> alphabet;
public MyCipher()
{
alphabet = new List<Code>();
alphabet.Add(new Code("4", " take 4"));
alphabet.Add(new Code(" ", " a"));
alphabet.Add(new Code("4d", " for 4 days"));
}
public string Cipher(params string[] codes)
{
StringBuilder result = new StringBuilder();
foreach (string code in codes)
{
Code element =
alphabet.Where(x => x.MyCode == code).SingleOrDefault();
if (element != null)
{
result.Append(element.MyDecoded);
}
}
return result.ToString();
}
class Code
{
public string MyCode;
public string MyDecoded;
public Code(string code, string decode)
{
MyCode = code;
MyDecoded = decode;
}
}
}
按钮代码
public partial class Form1 : Form
{
private ICipherDecipher myCipher;
public Form1()
{
myCipher = new MyCipher();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string textToBeCiphered = textBox1.Text;
string textCiphered = myCipher.Cipher(textToBeCiphered);
textBox2.Text = textCiphered;
}
}
答案 0 :(得分:0)
您要遍历字符串(text
),因此code
变量应该是char
,而不是string
:
foreach( char code in text )
或者,您可以使用var
:
foreach( var code in text )
使编译器自动分配它认为应该的类型,但是您仍然应该知道它实际上是 的类型,因为它会影响变量支持的操作。
再深入一点,其他一些语言(尤其是像JavaScript和Python这样的动态语言)没有区别,但是C#(像大多数其他类似C的语言一样)的数据类型为char
,文本的单个元素。对于C#,该类型保存一个UTF-16代码单元。 (这并不总是与用户可见的字符相同,但这是一个完整的 other 故事)。
根据评论中的讨论,您似乎希望匹配整个字符串而不是字符串中的字符。在这种情况下,根据您要执行的操作,您有两种选择:
如果您希望Cypher方法接收单个字符串并获取与该字符串匹配的代码,只需摆脱循环并直接与参数匹配:
private string Cipher( string code )
{
StringBuilder result = new StringBuilder();
Code element =
alphabet.Where(x => x.MyCode == code).SingleOrDefault();
if ( element != null)
{
result.Append(element.MyDecoded);
}
return result.ToString();
}
或者,如果您要传递多个字符串并处理所有字符串,请传递IEnumerable<string>
(来自System.Collections.Generic
)或派生类型,例如{{1} }:
string[]
或者,如果将 private string Cipher( IEnumerable<string> codes )
{
StringBuilder result = new StringBuilder();
foreach( string code in codes )
{
Code element =
alphabet.Where(x => x.MyCode == code).SingleOrDefault();
if ( element != null)
{
result.Append(element.MyDecoded);
}
}
return result.ToString();
}
// Usage examples:
private string CallSite() {
// (These are just some of the options, other enumerable types
// will work as well as long as they enumerate over strings)
return Cypher( new[] { "Aca", "Bb" });
return Cypher( new List<string> { "Aca", "Bb" });
}
替换为IEnumerable<string>
,则可以调用该函数,就好像它具有任意数量的字符串参数一样:
params string[]
在上述任何一种情况下,都可以用 private string Cipher( params string[] codes )
{
// ... (as above)
}
// Usage example:
private string CallSite() {
return Cypher( "Aca", "Bb" );
}
替换code.ToString()
,因为它已经 是一个字符串(而code
只是返回自身)。
答案 1 :(得分:0)
更改代码的这一部分:
foreach( char code in text )
{
Code element =
alphabet.Where(x => x.MyCode == code.ToString()).SingleOrDefault();
if ( element != null)
{
result.Append(element.MyDecoded);
}
}
然后在所有使用代码的地方,都使用code.ToString()