如何在C#中将输入字符串转换为大写 [英] How to convert an input string to uppercase in c#

查看:201
本文介绍了如何在C#中将输入字符串转换为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string choice = String.ToUpper(Console.ReadLine());

我想输入一个字符串并将其转换为大写。但是,有一个错误指出:

I want to input a string and convert it to upper case. However, there's an error that states :


无法从'string'转换为System.Globalization.CultureInfo'

cannot convert from 'string' to System.Globalization.CultureInfo'

,当我将鼠标悬停在 Console.ReadLine()上时出现。为什么这样行不通,并且有哪些修复程序?还有另一种方法吗?

that appears when I hover over the Console.ReadLine(). Why doesn't this work , and what fixes are there? And is there another way to do this ?

推荐答案

String.ToUpper 是一个实例方法,这意味着您必须在字符串上使用:

String.ToUpper is an instance method, that means you have to use it "on" your string:

string input = Console.ReadLine();
string choice = input.ToUpper();

否则,您将使用超载,该超载采用 CultureInfo 对象。由于 String 无法转换为 System.Globalization.CultureInfo ,因此会出现编译器错误。但这仍然会引起误解,您不能在没有实例的情况下使用实例方法,因此会产生另一个错误:

Otherwise you are using the overload that takes a CultureInfo object. Since String is not convertible to System.Globalization.CultureInfo you get the compiler error. But it's misleading anyway, you can't use an instance method without instance, so this yields another error:

String.ToUpper(CultureInfo.CurrentCulture);  // what string you want upper-case??!




非静态字段,方法,或
属性'string.ToUpper(CultureInfo)

An object reference is required for the non-static field, method, or property 'string.ToUpper(CultureInfo)

仅在方法没有类型实例的情况下可以使用是静态

A method can be used without an instance of the type only if it is static.

这篇关于如何在C#中将输入字符串转换为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆