将句子修改为单独的单词

时间:2016-03-31 04:18:06

标签: c#

在console.clear之后的一段代码是我遇到麻烦的地方。我运行我的程序,然后出现句子的其他部分大写的单词。我必须将句子分成单独的单词。

将每个单词的第一个字母大写,然后将单词连接回单个变量。代码以粗体显示

using System;
using System.Threading.Tasks;

namespace The_Quick_Brown_Fox
{
    class Program
    {
        static void Main()
        {

            string copyOne = "the quick  brown fox jumps over the lazy dog";
            string hairy = "hairy";
            string copyTwo;

            copyTwo = string.Copy(copyOne);
            copyTwo = copyTwo.Replace("dog", "chicken");
            copyTwo = copyTwo.Insert(10, hairy);
            copyTwo = copyTwo.TrimEnd();

            Console.WriteLine(copyOne);
            Console.WriteLine();
            Console.WriteLine("" + copyTwo + "");

            Console.ReadLine();
            Console.Clear();

            string lower = (copyTwo);
            Console.WriteLine(lower.ToUpper());

            Console.ReadLine();
            Console.Clear();

            string upper = (copyTwo);
            Console.WriteLine(upper.ToLower());

            Console.ReadLine();
            Console.Clear();

            copyTwo = string.Copy(copyTwo);
            copyTwo = copyTwo.Replace("e", "y");

            Console.WriteLine("" + copyTwo + "");

            Console.ReadLine();
            Console.Clear();

            string[] names = { "Krissi", "Dale", "Bo", "Christopher" };
            double[] wealth = { 150000, 1000000, 5.66, 10 };

            Console.Write("names".PadRight(15));
            Console.WriteLine("wealth".PadLeft(8));

            for (int i = 0; i < 4; i++)
            {
                Console.Write(names[i].PadRight(15));
                Console.WriteLine(wealth[i].ToString().PadLeft(8));
            }

            Console.ReadLine();
            Console.Clear();

            **string wordThree = "the brown fox jumps over the lazy dog";
            string[] split = wordThree.Split(' ');
            wordThree = wordThree.Replace("dog", "chicken");
            wordThree = wordThree.Insert(10, hairy);
            wordThree = wordThree.TrimEnd();

            Console.WriteLine(wordThree);
            Console.WriteLine();
            Console.WriteLine("" + wordThree + "");

            foreach (string item in split)
            {
            wordThree = wordThree + item[0].ToString().ToUpper() 
                + item.Substring(1) + " ";
            }
            wordThree = wordThree.Trim();
            Console.WriteLine(wordThree + " ");
            Console.ReadLine();**


        }
    }
}

3 个答案:

答案 0 :(得分:0)

我认为这会解决你的问题吗?我添加了一个名为Finalword的新字符串变量,我在这里添加了你在wordthree上操作的新句子。看看这个

   static void Main()
   {

        string copyOne = "the quick  brown fox jumps over the lazy dog";
        string hairy = "hairy ";
        string copyTwo;

        copyTwo = string.Copy(copyOne);
        copyTwo = copyTwo.Replace("dog", "chicken");
        copyTwo = copyTwo.Insert(10, hairy);
        copyTwo = copyTwo.TrimEnd();

        Console.WriteLine(copyOne);
        Console.WriteLine();
        Console.WriteLine("" + copyTwo + "");

        Console.ReadLine();
        Console.Clear();

        string lower = (copyTwo);
        Console.WriteLine(lower.ToUpper());

        Console.ReadLine();
        Console.Clear();

        string upper = (copyTwo);
        Console.WriteLine(upper.ToLower());

        Console.ReadLine();
        Console.Clear();

        copyTwo = string.Copy(copyTwo);
        copyTwo = copyTwo.Replace("e", "y");

        Console.WriteLine("" + copyTwo + "");

        Console.ReadLine();
        Console.Clear();

        string[] names = { "Krissi", "Dale", "Bo", "Christopher" };
        double[] wealth = { 150000, 1000000, 5.66, 10 };

        Console.Write("names".PadRight(15));
        Console.WriteLine("wealth".PadLeft(8));

        for (int i = 0; i < 4; i++)
        {
            Console.Write(names[i].PadRight(15));
            Console.WriteLine(wealth[i].ToString().PadLeft(8));
        }

        Console.ReadLine();
        Console.Clear();

        string wordThree = "the brown fox jumps over the lazy dog";
        wordThree = wordThree.Replace("dog", "chicken");
        wordThree = wordThree.Insert(10, hairy);
        wordThree = wordThree.TrimEnd();
        string[] split = wordThree.Split(' ');
        Console.WriteLine(wordThree);
        Console.WriteLine();
        Console.WriteLine("" + wordThree + "");
        string FinalWord = "";
        foreach (string item in split)
        {
            FinalWord = FinalWord + item[0].ToString().ToUpper()
            + item.Substring(1) + " ";
        }
        FinalWord = FinalWord.Trim();
        Console.WriteLine(FinalWord + " ");
        Console.ReadLine();


    }

答案 1 :(得分:0)

以下代码将帮助您将给定的字符串转换为首字母大写。

string copyThree = string.Join(" ", copyOne.Split(' ').Select(s => s.Length > 0 ? s[0].ToString().ToUpper() + s.Substring(1) : s));

答案 2 :(得分:0)

  

将每个单词的第一个字母大写,然后将单词连接回单个变量。

其中任何一个都适合你。

var wordThree = string.Join(" ", input.Split(new char[] {' ' })
                                      .Select(s=>  s.Substring(0,1).ToUpper() + s.Length > 1? s.Substring(1) : ""))

或者这个,它将其他字母保留在lowercase

 var culture = new CultureInfo("en-US");

 var wordThree = string.Join(" ", wordThree.Split(new char[] {' ' })
                                           .Select(s=> culture.TextInfo.ToTitleCase(s.ToLower())));

工作Example