我正在学习C#,但有一件事让我很沮丧。我正在学习字符串方法及其工作方式。
public static void CaseFlip()
{
Console.WriteLine(" CaseFlip -- Output");
Console.WriteLine("==============================================================================");
for (int i = 0; i < text.Length; i ++)
{
char[]delimiters = {'/'};
string[]splitString = text.Split(delimiters);
for (int k = 0; k < splitString.Length; k +=3)
{
string sub = text.Substring(0);
string remove = sub.Remove(4, text.Length);
string insert = remove.Insert(0, sub);
splitString[k] = splitString[k].ToUpper();
Console.WriteLine(splitString[k]);
}
}
Console.WriteLine(" ");
}
我的字符串数组是:
static string text = "You only have one chance to make a first impression/" +
"Consider the source and judge accordingly/" +
"You can do something for a day you can't imagine doing for a lifetime/" +
"Are we not drawn onward, we few, drawn onward to new era/" +
"Never odd or even/" +
"Madam, I'm Adam/" +
"What do you mean? It's not due tomorrow, is it?";
该怎么办?
不需要第一个for
循环;单个循环应该迭代文本中的分隔符数量。此外,您将在这一行中获得一个异常
string remove = sub.Remove(4, text.Length);
因为您试图通过删除整个文本的一部分来创建一个新字符串,从第4个字符开始,并使用与文本一样多的字符。长度-有效地超出界限。试试这个:
public static void CaseFlip(string text)
{
Console.WriteLine(" CaseFlip -- Output");
Console.WriteLine("==============================================================================");
char[] delimiters = { '/' };
string[] splitString = text.Split(delimiters);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splitString.Length; i++)
{
char[] charsInLine = splitString[i].ToCharArray();
for (int k = 0; k < charsInLine.Length; k++)
{
sb.Append(k % 3 == 0 ? char.ToUpper(charsInLine[k]) : charsInLine[k]);
}
sb.Append(' ');
}
Console.WriteLine(sb.ToString());
Console.WriteLine(" ");
}
对于这样的字符串操作,您应该考虑使用StringBuilder类。要使用它,只需将using System.Text
添加到文件顶部即可。
您可以使用Substring
选择要转换为大写的字符,然后使用Remove
从字符串中删除原始字符,使用Insert
将大写字符添加回。因为字符串是不可变的,您还需要将中间步骤存储在变量中,以便将更改转移到下一个周期。
public static void CaseFlip()
{
Console.WriteLine(" CaseFlip -- Output");
Console.WriteLine("==============================================================================");
var splitString = text.Split('/');
for (var i = 0; i < splitString.Length; i++)
{
var line = splitString[i];
for (var k = 0; k < line.Length; k += 3)
{
var upperCasedCharacter = line.Substring(k, 1).ToUpper();
line = line.Remove(k, 1).Insert(k, upperCasedCharacter);
}
Console.WriteLine(line);
}
Console.WriteLine(" ");
}
以下是我对此的看法:
string text =
"You only have one chance to make a first impression/" +
"Consider the source and judge accordingly/" +
"You can do something for a day you can't imagine doing for a lifetime/" +
"Are we not drawn onward, we few, drawn onward to new era/" +
"Never odd or even/" +
"Madam, I'm Adam/" +
"What do you mean? It's not due tomorrow, is it?";
string result = new string(
text
.Select((c, i) => i % 3 == 0 ? char.ToUpper(c) : c)
.ToArray());
你在战争中无法想象,我们很少,RawN onWarD to New erA/NEveR oDd Or EveN/MAdaM,我是AdAm/WhaT Do You MeaN?不是因为MorRow,是吗?
SubString
、Remove
和Insert
都会创建一个新的字符串,并且不会占用内存。
实现所需内容的一种简单方法是使用ToCharArray
将字符串转换为char数组,然后使用简单的for循环进行迭代。在循环中,您可以检查该位置的字符是否为字母且大于或等于ASCII 97。小写字母表从97开始。
将小写字母转换为大写字母就是从字母中减去32。由于您正在修改一个char数组,因此不会在内存中创建任何新的字符串。
完成大写后,可以使用string构造函数之一再次将char数组转换为字符串。
var chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i += 3)
{
if (char.IsLetter(chars[i]) && chars[i] >= 'a')
chars[i] = (char)(chars[i] - 32);
}
text = new string(chars);