C# 字符增量
C# Character Incrementation
我是 C# 的新手,所以这个问题(希望如此)会非常简单。
我正在尝试递增字符数组中的每个字符。这是我的代码。
//increment each character in array
for (int i = 0; i < text.Length; i++)
{
textArray[i]++; //this works
// textArray[i] +=13; //this doesn't work
}
我可以将数组递增 1,但不能超过这个数。
谢谢!
如果有帮助,这是我的其余代码。
// put all text into a string - here loosely typed as a var
var s = System.IO.File.ReadAllText(@"C:\Users\Eliezer Feder\Documents Landers\Limudie Cholth Semester\C#\GettysburgAddress.txt");
var upper = s.ToUpper();
string text = ""; //empty string to add characters to it if they are indeed characters.
foreach (char c in upper.ToCharArray())
{
if (Char.IsLetter(c))
{
text += c;
}
}
//change the 'text' string to an array so can increment each individual char
Char[] textArray = text.ToArray();
//output old text in the char array:
Console.WriteLine(textArray);
Console.ReadKey();
//increment each character in array
for (int i = 0; i < text.Length; i++)
{
textArray[i]++; //this works
// textArray[i] +=13; //this doesn't work
}
Console.WriteLine(textArray);
Console.ReadKey();
//change back to string so can write to file:
string lines = ""; //empty string to add characters to it if they are characters.
foreach (char c in upper.ToCharArray())
{
lines += textArray[c];
}
System.IO.File.WriteAllLines(@"Eliezer Feder\Documents Landers\Limudie Cholth Semester\C#\encrypted.txt", lines); //THIS PART IS ALSO NOT WORKING FOR SOME REASON
textArray
的元素类型是 char
。文字 13
的类型是 int
。 char
加上int
的结果是int
,所以不能赋值给char
变量。
您必须将文字转换为 char
,那么结果也将是 char
:
textArray[i] += (char)13;
我是 C# 的新手,所以这个问题(希望如此)会非常简单。
我正在尝试递增字符数组中的每个字符。这是我的代码。
//increment each character in array
for (int i = 0; i < text.Length; i++)
{
textArray[i]++; //this works
// textArray[i] +=13; //this doesn't work
}
我可以将数组递增 1,但不能超过这个数。
谢谢!
如果有帮助,这是我的其余代码。
// put all text into a string - here loosely typed as a var
var s = System.IO.File.ReadAllText(@"C:\Users\Eliezer Feder\Documents Landers\Limudie Cholth Semester\C#\GettysburgAddress.txt");
var upper = s.ToUpper();
string text = ""; //empty string to add characters to it if they are indeed characters.
foreach (char c in upper.ToCharArray())
{
if (Char.IsLetter(c))
{
text += c;
}
}
//change the 'text' string to an array so can increment each individual char
Char[] textArray = text.ToArray();
//output old text in the char array:
Console.WriteLine(textArray);
Console.ReadKey();
//increment each character in array
for (int i = 0; i < text.Length; i++)
{
textArray[i]++; //this works
// textArray[i] +=13; //this doesn't work
}
Console.WriteLine(textArray);
Console.ReadKey();
//change back to string so can write to file:
string lines = ""; //empty string to add characters to it if they are characters.
foreach (char c in upper.ToCharArray())
{
lines += textArray[c];
}
System.IO.File.WriteAllLines(@"Eliezer Feder\Documents Landers\Limudie Cholth Semester\C#\encrypted.txt", lines); //THIS PART IS ALSO NOT WORKING FOR SOME REASON
textArray
的元素类型是 char
。文字 13
的类型是 int
。 char
加上int
的结果是int
,所以不能赋值给char
变量。
您必须将文字转换为 char
,那么结果也将是 char
:
textArray[i] += (char)13;