[英]Split a string after a word with up to 20 characters on a line
I am trying to write a program which will split a user input of a very long sentence which every 20 characters, a new line is created.我正在尝试编写一个程序,它将一个非常长的句子的用户输入拆分为每 20 个字符,创建一个新行。 If the end of the 20 characters is in the middle of a word, it will print the previous full word如果这 20 个字符的末尾在一个单词的中间,它将打印前一个完整的单词
using System;
namespace stringToLines
{
class Program
{
static void split()
{
string sent = Console.ReadLine();
int a = sent.Length / 20;
string[] line;
int space = sent.IndexOf(' ');
while (space < 20)
{
int i = 0;
line[i] = sent.Substring(0, space);
sent = sent.Substring(space, sent.Length-space);
space = sent.IndexOf(' ');
i++;
}
for (int i = 0; i <=a; i++)
{
Console.WriteLine(line[i],"\n");
}
Console.ReadLine();
return split;
}
static void Main(string[] args)
{
Console.WriteLine("Please enter your long sentence(>20 characters)");
split();
}
}
}
Your code includes a lot of errors.您的代码包含很多错误。 Some main errors are:一些主要错误是:
I would recommend you to read the Getting started documentation of .NET我建议您阅读.NET的入门文档
To getting started with your current problem, you can take look at the following code.要开始解决您当前的问题,您可以查看以下代码。 I added comments to explain whats happening:我添加了评论来解释发生了什么:
using System;
using System.Collections.Generic;
using System.Text;
namespace stringToLines
{
class Program
{
static void split()
{
string sent = Console.ReadLine(); //Read the input
string[] split = sent.Split(' '); // Get the words of the input
StringBuilder builder = new StringBuilder();
int charCounter = 0;
List<string> lines = new List<string>();
foreach (string word in split) //Iterate through the words
{
charCounter += word.Length; //Add the word to the charCounter
if (charCounter >= 20) { // Check if the charCounter exceeds the limi´t
lines.Add(builder.ToString().TrimEnd()); //Add the buffered line into the result list
builder.Clear(); // Clear the buffer
charCounter = word.Length; //Reset the char counter to the current length
}
builder.Append(word + " "); //Add the word plus a space to the buffer
}
lines.Add(builder.ToString().TrimEnd()); //Add Last Line
foreach (string line in lines) //Iterate through the result list
Console.WriteLine(line); // Print the result
Console.ReadLine();
}
static void Main(string[] args)
{
Console.WriteLine("Please enter your long sentence(>20 characters)");
split();
}
}
}
You don't ask a very clear question.你没有问一个很明确的问题。 I read that you want to break an entered text into lines that are a maximum of 20 characters long, but you don't want to break into a word itself.我读到您想将输入的文本分成最多 20 个字符长的行,但您不想将其分成单词本身。 If that's your question, you can use the solution below:如果这是您的问题,您可以使用以下解决方案:
Change your code:更改您的代码:
static void Main(string[] args)
{
Console.WriteLine("Please enter your long sentence(>20 characters)");
var sent = Console.ReadLine();
var lines = sent.SplitOnLastWhiteSpaceAt(20);
for (int i = 0; i <= lines.Length; i++)
{
Console.WriteLine(lines[i], "\n");
}
Console.ReadLine("Press any key to close");
}
Use this string extension methode:使用此字符串扩展方法:
using System.Collections.Generic;
namespace stackoverflow.question_69882038.Extensions
{
public static class StringExtensions
{
public static string[] SplitOnLastWhiteSpaceAt(this string value, int maxLength)
{
List<string> values = new List<string>();
try
{
var _rest = value;
while (_rest.Length > maxLength)
{
for (var i = maxLength; i >= 0; i += -1)
{
//Start on end, Search last index of WhiteSpace
if (char.IsWhiteSpace(_rest[i]))
{
values.Add(_rest.Substring(0, i));
if (i > _rest.Length)
return values.ToArray();
//Last item is not Empty or WithSpace
_rest = _rest.Substring(i + 1);
//Remove the WithSpace where splitted on
break;
//No Whitespace was found. Split on maxLenght.
}
else if (i == 0)
{
values.Add(_rest.Substring(0, maxLength));
_rest = _rest.Substring(maxLength);
break;
}
}
}
if (_rest.Length > 1 || (_rest.Length > 0 && !char.IsWhiteSpace(_rest[0])))
values.Add(_rest);
//Last item is not 1 Char as Empty or WithSpace
return values.ToArray();
}
finally
{
values.Clear();
value = null;
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.