如何通过从键盘读取名称和姓氏并仅使姓氏字母变大来打印?
string name;
Console.Write("Name and surname:");
name=Console.ReadLine();
int word= name.LastIndexOf(" ");
Console.WriteLine(name.Substring(word).ToUpper());
我可以写姓,但不能添加名字。对不起,我的英语。.
答案 0 :(得分:2)
我会使用.Split()
来分隔姓氏和名字
Console.Write("Name and surname:");
string name = Console.ReadLine();
Console.WriteLine(name.Split().First().ToUpper());
Console.WriteLine(name.Split().Last().ToUpper());
如果您想呆在Substring()
上,这就是方法
Console.Write("Name and surname:");
string name = Console.ReadLine();
int word = name.LastIndexOf(" ");
Console.WriteLine(name.Substring(word+1).ToUpper());
Console.WriteLine(name.Substring(0, word).ToUpper());
答案 1 :(得分:1)
我解决了我的问题。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;/*Add this library for ToTitleCase*/
namespace String_denemeler
{
class Program
{
public static string ToTitleCase(string Text)/*And Add this for ToTitleCase*/
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(Text);
}
static void Main(string[] args)
{
string name;
Console.Write("Name&Surname:");
name = Console.ReadLine();
name= ToTitleCase(name);
int index = name.LastIndexOf(" ");
Console.WriteLine(name.Substring(0, index) + name.Substring(index).ToUpper());
}
}
}