英文:
Trying to change first char element in string array
问题
我是相对新手程序员,我试图编写一个程序,将用户输入的任何内容转换为帕斯卡命名法。
处理字符串通常比较简单。但是,我遇到了一个问题,这个问题通过搜索互联网并没有找到容易的解决方法。将字符串数组中的每个元素都转换为小写是相对容易的,但确保字符串数组中每个元素的第一个字符都是大写的,这就比较棘手了。
我在这里提出问题,而不是仅仅查看解决方案(这是一个有解决方案的课程问题),是因为我想知道我编写的代码中是否有解决方法。
static void Main(string[] args)
{
Console.WriteLine("输入你想要转换为帕斯卡命名法的单词。");
var input = Console.ReadLine();
var inputLower = input.ToLower();
var inputLowerSplit = inputLower.Split(' ');
for (var i = 0; i < inputLowerSplit.Length; i++)
{
var write = inputLowerSplit[i].ToCharArray();
Char.ToUpper(write[0]);
Console.Write(write);
}
}
问题出现在第14行,即 Char.ToUpper(write[0]);
处。到目前为止,我只得到了所有强制小写且没有空格的字符串输入,这没问题,但每个单词的第一个字母不是大写的,这不符合我代码运行的期望。
在线搜索没有得到任何答案。在我编写的代码中是否有可能解决这个问题,还是我需要采用不同的方法?感觉我已经接近了解决方案。
英文:
I am fairly new to programming, and I was trying to write a program that rewrites any input from the user to be in pascal case.
Working with strings is mostly straightforward. However, I ran into an issue, and one that does not have an easy fix looking through the Internet. Getting -every- element in a string array to be lowercase is easy enough, but making sure that the first char element in an element of a string array to be uppercase is pretty tricky.
I'm asking here instead of just checking the solution (It's a question to a course that has a solutions packet) if there is a solution within the code I wrote up.
static void Main(string[] args)
{
Console.WriteLine("Write words that you would like to be in pascal case.");
var input = Console.ReadLine();
var inputLower = input.ToLower();
var inputLowerSplit = inputLower.Split(' ');
for (var i = 0; i < inputLowerSplit.Length; i++)
{
var write = inputLowerSplit[i].ToCharArray();
Char.ToUpper(write[0]);
Console.Write(write);
}
}
Line 14, where Char.ToUpper(write[0]); is, is where I am having the issue. So far I just get the string input where it's all forcibly lowercase and without spaces, which is fine, but the first letter of each word is not uppercase like how I am expecting the code to run.
Looking online does not yield any response. Is it possible within the code I wrote up or do I just need to take on a different approach? Felt like I was on to something.
答案1
得分: 0
将要翻译的部分是:
A quick solution is to assign the char you want
write[0] = char.ToUpper(write[0]);
This will return the char as uppercase in the position you want.
英文:
A quick solution is to assign the char you want
write[0] = char.ToUpper(write[0]);
This will return the char as uppercase in the position you want.
答案2
得分: 0
听起来用户正在输入一个包含多个单词的字符串,并且您希望将每个单词的首字母大写,其余部分小写。在这种情况下,您应该先将整个字符串转换为标题大小写,然后再进行拆分。可以使用TextInfo.ToTitleCase
方法实现这一点,例如:
var input = Console.ReadLine();
var words = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input).Split(' ');
Console.WriteLine(string.Join(Environment.NewLine, words));
使用上面的输入,该代码产生了以下输出:
Hello
World
Goodbye
这里使用了不变的文化,但您可以使用适当的文化。您甚至可以将该功能集成到一个或多个扩展方法中,模仿ToUpper
和ToLower
的功能:
public static class StringExtensions
{
public static string ToTitle(this string source)
{
return source.ToTitle(CultureInfo.CurrentCulture);
}
public static string ToTitle(this string source, CultureInfo culture)
{
return culture.TextInfo.ToTitleCase(source);
}
public static string ToTitleInvariant(this string source)
{
return source.ToTitle(CultureInfo.InvariantCulture);
}
}
然后,您可以这样使用:
var words = Console.ReadLine().ToTitle().Split();
请注意,无需指定在哪些字符上拆分,因为Split
默认会在空格上拆分。
英文:
It sounds like the user is entering a string
containing multiple words and you want to capitalise the first letter of each word and make everything else lower case. In that case, you should convert the entire string
to title case first, then split it. The TextInfo.ToTitleCase
method can be used for this, e.g.
var input = Console.ReadLine();
var words = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(input).Split(' ');
Console.WriteLine(string.Join(Environment.NewLine, words));
With the input below, that code produced the subsequent output:
<pre>
hello WoRlD goodBye
Hello
World
Goodbye
</pre>
That uses the invariant culture but you can use whatever culture is appropriate. You could even incorporate that functionality into one or more extension methods that mimic ToUpper
and ToLower
:
public static class StringExtensions
{
public static string ToTitle(this string source)
{
return source.ToTitle(CultureInfo.CurrentCulture);
}
public static string ToTitle(this string source, CultureInfo culture)
{
return culture.TextInfo.ToTitleCase(source);
}
public static string ToTitleInvariant(this string source)
{
return source.ToTitle(CultureInfo.InvariantCulture);
}
}
You can then just do this:
var words = Console.ReadLine().ToTitle().Split();
Note that there's no need to specify to split on spaces because Split
will split on whitespace by default.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论