简体   繁体   English

每个超过 3 个字母的单词都以大写开头 + 第一行是大写 - 快速问题

[英]Every word longer than 3 letter starts with capitalized + first line is Upper case - quick question

I got assignment to do first line in upper case and in the rest every word that is 3 or more characters long starts with upper case.我得到了第一行大写的任务,其余的每个 3 个或更多字符长的单词都以大写开头。

I am testing it in console so its made partly with streamreader/streamwriter, but when it functions well I will correct it.我正在控制台中测试它,所以它部分是用流阅读器/流编写器制作的,但是当它运行良好时我会纠正它。

Problem is that the Console.WriteLine();问题是Console.WriteLine(); before for loop with it, it makes an empty line after the first line but without it, it does not make 3rd line.在 for 循环之前,它在第一行之后创建一个空行,但没有它,它不会成为第三行。 The input text looks like this输入文本看起来像这样

title of text
sssssss ss ssss, ss sssssss
ddddd ddd ddddd dddddd

with the WriteLine it ends up like this:使用 WriteLine,它最终是这样的:

TITLE OF TEXT

sssssss ss ssss, ss sssssss
ddddd ddd ddddd dddddd

and without:并且没有:

TITLE OF TEXT
Sssssss ss Ssss, ss Sssssss Ddddd Ddd Ddddd Dddddd

I think I made it too complicated and got kinda lost so I am looking for the solution to this small problem or also to make this code easier but thats not the point right now.我想我把它弄得太复杂了,有点迷路了,所以我正在寻找这个小问题的解决方案,或者让这段代码更容易,但这不是现在的重点。

using System;
using System.IO;

namespace priklad_8._4
{
    class Program
    {
        static void Main(string[] args)
        {
            firstUpper(@"aaa.txt", @"new.txt");

            Console.ReadLine();
        }
        static void firstUpper(String from, String to)
        {
            StreamReader sr = new StreamReader(from);
            StreamWriter sw = new StreamWriter(to);
            String s;
            int length = 0;
            char[] pole = new char[100];
            char dd = 'A';
            while ((s = sr.ReadLine()) != null)
            {
                if (length == 0)
                {
                    Console.WriteLine(s.ToUpper());
                    length++;
                }
                else
                {
                    String[] ss = s.Trim(' ').Split(' ');

                    Console.WriteLine();
                    for (int i = 0; i < ss.Length; i++)
                    {
                        if (ss[i].Length >= 3)
                        {
                            String ds = ss[i];
                            char[] das = ds.ToCharArray();

                            Console.Write(Char.ToUpper(das[0]) + ds.Substring(1) + " ");
                            if ((int)dd == 10)
                            {

                                Console.WriteLine();

                            }
                        }
                        else Console.Write(ss[i] + " ");
                    }
                }
            }
            sw.Close();
            sr.Close();
        }
    }
}

Thank you!谢谢!

If you want to leave everything else as it is, just instead of如果您想保持其他一切不变,只需

Console.WriteLine(s.ToUpper());

use this:用这个:

Console.Write(s.ToUpper())

Maybe a cleaner way to get there:也许是一种更清洁的方式:

using (var input = new StreamReader("input.txt"))
{
    var currentLine = 0;
    while (!input.EndOfStream)
    {
        var line = input.ReadLine() ?? "";
        if (++currentLine == 1)
        {
            // first line is upper case
            Console.WriteLine(line.ToUpper());
        }
        else
        {
            // Every word longer than 3 letter starts with capitalized
            Console.WriteLine(Regex.Replace(line, @"\w{3,}", 
                (match) => CultureInfo.CurrentCulture
                    .TextInfo.ToTitleCase(match.Value.ToLower())));
        }
    }
}

Another Cleaner way to get there making use of File.ReadAllLines, and File.WriteAllLines使用 File.ReadAllLines 和 File.WriteAllLines 到达那里的另一种更清洁的方法

static void Main(string[] args)
{
    var lines = File.ReadAllLines("text.txt");
    var newLines = new string[lines.Count()];

    for (var i = 0; i < lines.Count(); i++)
    {
        if (i == 0)
        {
            newLines[i] = lines[i].ToUpper();
        }
        else
        {
            foreach (var word in lines[i].Split(' '))
            {
                newLines[i] += string.Format("{0} ", word.Length >= 3 ? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(word) : word);
            }
        }
    }

    File.WriteAllLines("newText.txt", newLines);
}

And i mean, if you wanted a very unclean looking one liner because i was bored.. You could do this.我的意思是,如果你因为我很无聊而想要一个看起来很不干净的衬里……你可以这样做。

static void Run(string fromFile, string toFile)
{
    File.WriteAllLines(toFile, File.ReadAllLines(fromFile).Select((line, index) => index == 0 ? line.ToUpper() : Regex.Replace(line, @"\w{3,}", (match) => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(match.Value))));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用mysql函数的情况下使每个单词的首字母大写? - How to make first letter upper case in every word without using functions in mysql? 将所有第一个字母转换为大写,每个单词的其余部分都较低 - Convert all first letter to upper case, rest lower for each word 验证用户仅输入一个单词的答案,并且首字母大写 - Validating user only enters a one word answer and that the first letter is capitalized 使文本框的第一个字母大写 - Making first letter of a textbox upper case 正则表达式每个单词的首字母大写由空格和 3 到 29 个字符长...C# - Regex first letter of each word Upper Case separated by spaces and 3 to 29 character long…C# 除了任何单词的第一个字母外,如何在字符串中大写特定的两个字母的单词? - How do I capitalized specific two letter word in a string in addition to the first letter of any word? 除了任何单词的第一个字母,我如何将字符串中的两个字母的单词都大写? - How do I capitalized any two letter word in a string in addition to the first letter of any word? 输入必须是大写(第一个字母)和小写(其他) - Input must be upper case(first letter) and lower case (others) 尝试创建一种方法,按小写首字母升序排序句子,然后按大写首字母降序排序 - Trying to create method that sorts a sentence in ascending order by lower case first letter then descending by upper case first letter 使字符串的第一个字母大写(具有最高性能) - Make first letter of a string upper case (with maximum performance)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM