[英]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.