简体   繁体   English

字符串数组上下字母更改C#

[英]string array upper and lower letters change C#

I need to print an array of strings line by line, where the first line has to be with uppercase letters, the second line with lowercase letters and thats how it goes until the end of the array (I get the array from a text file); 我需要逐行打印一个字符串数组,其中第一行必须使用大写字母,第二行必须使用小写字母,这就是直到数组结尾的方式(我从文本文件中获取数组) ;

When I try to use to use " chars[i].ToUpper " I get the error 当我尝试使用“ chars[i].ToUpper ”时出现错误

"Error 1 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement C:\\Users\\Yonatan\\Documents\\Visual Studio 2013\\Projects\\Clab2\\Clab2\\ex3.cs 21 17 Clab2" “错误1只有赋值,调用,递增,递减,等待和新对象表达式可以用作语句C:\\ Users \\ Yonatan \\ Documents \\ Visual Studio 2013 \\ Projects \\ Clab2 \\ Clab2 \\ ex3.cs 21 17 Clab2”

this is my code: 这是我的代码:

static void Main(string[] args)
{
    string fileContent = File.ReadAllText("FreeText.txt");
    string[] chars = fileContent.Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);


    for(int I =0;i<chars.Length;i++)
    {
        if (i % 2 != 0)
            chars[i].ToUpper;

    }
}

the first line has to be with uppercase letters,the second line with lowercase letters 第一行必须使用大写字母,第二行必须使用小写字母

You have to overwrite the string in the array with the upper/lower-case strings: 您必须使用大写/小写字符串覆盖数组中的字符串:

if (i % 2 != 0)
    chars[i] = chars[i].ToUpper(); 
else
    chars[i] = chars[i].ToLower(); 

Replace 更换

chars[i].ToUpper();

to

chars[i] = chars[i].ToUpper();

ToUpper is a method, not a property. ToUpper是一种方法,而不是属性。 Also you have to reassign it, because ToUpper will return a new string. 另外,您还必须重新分配它,因为ToUpper将返回一个新字符串。

Try with this: 试试这个:

for (int i = 0; i < chars.Length; i++) 
{
    if (i % 2 != 0)
    {
        chars[i] = chars[i].ToUpper();
    }
    else
    {
        chars[i] = chars[i].ToLower();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#计算字符串中大写和小写字母的数量 - C# Counting the number of upper and lower case letters in a string 在C#中获取整数的上下字节并将其作为char数组发送到com端口,如何? - Getting upper and lower byte of an integer in C# and putting it as a char array to send to a com port, how? 如何从字符串数组创建字符字符数组? (C#) - How to create char array of letters from a string array ? (C#) c#:以大写和小写形式搜索DataGridView列文本 - c#: Search the DataGridView columns text both in Upper and Lower cases 在C#中没有上限和下限的情况下绘制折线图 - Drawing line chart without lower and upper bound in c# 在聊天中阻止字符串大写/小写C# - Blocking Strings in a chat Upper/Lower case C# 具有动态上限和下限的MSChart C#错误条形图 - MSChart C# Error Bar Graph With Dynamic Upper and Lower Bounds 如何将一个句子分成字母并在C#中存储为一个String数组? - how to split a sentence into letters and store as a String array in C#? 有没有更好的方法从C#中的大写字母创建首字母缩略词? - Is there a better way to create acronym from upper letters in C#? 匹配给定字符串中所有不包含小写字母或仅包含大写字母的单词 - Match all words from given string where words does not have only lower case letters or only upper case letters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM