2012-07-03 44 views
-2

对于奇怪的标题感到抱歉,我很难解释它。我想是的人物在我的字符串这样的比较:将数组中的字符串分解为C中的字符#

stringa = new string[3]; 

for (int x = 0; x < stringa.GetLength(0); x++) 
{ 
    Console.WriteLine("String" + (x + 1) + ":"); 
    stringa[x] = Console.ReadLine(); 
} 

for(int x = 0;x < stringa.GetLength(0);x++) 
{ 
    Console.WriteLine(stringa[x]); 
} 

我想的是,现在我有stringa [X],我想那个字符串中的每个字符进行比较。有没有什么办法可以做到这一点,而不使用任何临时变量?

+2

*我想比较该字符串内的每个字符*比较什么? – McGarnagle

+0

根据您发布的代码,您将获得3个字符串的数组。你想要比较什么? –

+0

你想把它和它比较? –

回答

2

您可以通过索引获取字符串中的字符。例如:

string myString = "Test string"; 
Console.WriteLine(myString[0]); 

会写的字“T”

在你的情况,你可以另一个用于循环的第二个里面并调用stringa[x][y]获得第j个字符stringa[x],但它更有效地存储stringa[x]在一个局部变量和指标,像这样:

for(int x = 0;x < stringa.Length(0);x++) 
{ 
    // Store stringa[x] so that we don't have to keep indexing stringa 
    string stringx = stringa[x]; 
    for(int y = 0;y < stringx.Length(0);x++) 
    { 
     // Do some comparison on stringx[y] 
    } 
} 
0

字符串的每个字符都可以通过索引访问,所以在下面的节目1号线:

Console.WriteLine(stringa[0]); 

这段代码显示从1弦第1字符:

Console.WriteLine(stringa[0][0]); 

确定每个单独字符串的长度,为循环的目的,你可以找到具体的长度字符串如:

stringa[x].Length 
相关问题