Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 字符串c中的第一个大写字母#_C#_Uppercase_Letter - Fatal编程技术网

C# 字符串c中的第一个大写字母#

C# 字符串c中的第一个大写字母#,c#,uppercase,letter,C#,Uppercase,Letter,我正在尝试将字符串的第一个字母改为大写 我看到了关于这个的其他问题,但即使我应用了他们所说的,我仍然无法获得正确的结果 public string FirstLetterToUpper(string str) { if (str == null) return null; if (str.Length > 1) return char.ToUpper(str[0]) + str.Substring(1); return str.T

我正在尝试将字符串的第一个字母改为大写

我看到了关于这个的其他问题,但即使我应用了他们所说的,我仍然无法获得正确的结果

public string FirstLetterToUpper(string str)
{
    if (str == null)
        return null;

    if (str.Length > 1)
        return char.ToUpper(str[0]) + str.Substring(1);

    return str.ToUpper();
}

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = "test text";
    CapitalizeFirstLetter(label1.Text);        
}
而不是输出

Test text
它仍然存在

test text

有什么想法吗?

您需要指定结果(并使用正确的方法名称)


您没有使用
FirstLetterToUpper
方法的返回值。试试这个:

private void button1_Click(object sender, EventArgs e)
{
    label1.Text = "test text";
    label1.Text = FirstLetterToUpper(label1.Text);        
}

我不确定这是基于web还是基于桌面的应用程序。但是,我相信您的问题源于减速不良。例如,您的标签具有适当的值,但您没有重新指定该值

label1.Text = CapitalizeFirstLetter(label1.Text);
它不起作用的原因是,您没有更改标签的值,而是将其保留为:
testtext
。这就是你的问题发生的原因

但是,您可以通过执行以下操作使其更具可读性:

label1.Text = CapitalizeFirstLetter("text text");

其他答案解释了您的错误(您需要将方法获得的值重新分配给标签),但您也可以更改大写的代码以使用内置方法

string[] words = label1.Text.Split();
words[0] = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(words[0]);
label1.Text = string.Join(" ", words);

这对全球化问题更具弹性。

label1.Text=CapitalizeFirstLetter(“测试文本”)您需要能够处理所有unicode吗?一般来说,您不能使用第一个UTF-16代码单元和大写字母,因为一个符号可能包含多个代码单元,甚至多个代码点。
CapitalizeFirstLetter
方法在哪里?如果以这种方式实现,那么一切都会正常工作:
label1.Text=firstletttouper(输入)
string[] words = label1.Text.Split();
words[0] = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(words[0]);
label1.Text = string.Join(" ", words);