属性或索引器'string.this []'无法分配给它-只读c#(如果语句)[关闭]

问题描述 投票:0回答:1

[每次我使用text[i]都会返回上述错误。该功能应该删除主消息之前和之后的所有空格,因此,如果有人输入很多空格,他们将被删除。

public string ClearTheSpaces(string text)
{
    char space = ' ';

    for (int i = 0; i < text.Length; i++) //removing all spaces before the main message.
    {
        if (text[i] = space)
        {
            text.Remove(i, 1);
        }
        else break;
    }

    for (int i = text.Length; i > 0; i++) //removing all spaces after the message.
    {
        if (text[i] = ' ')
        {
            text.Remove(i, 1);
        }
        else break;
    }

    return text;
}
c# string indexer
1个回答
0
投票

您应该使用比较运算符==text[i] == space

不是=这是赋值运算符

© www.soinside.com 2019 - 2024. All rights reserved.