如果超过37个字符,如何将字符串拆分成多行?
例句
快速的棕色狐狸跳过懒狗
它应该成功
快速布朗福克斯跳过了 懒狗
虽然第37个字符是'L'
我想用文字分组。
这是我的代码
private string sentence(string statement)
{
string completedWord = "";
string temp = "";
string[] wordArray = statement.Split(' ');
for (int i = 0; i < wordArray.Length; i++)
{
temp = completedWord + wordArray[i] + ' ';
if (temp.Length < 37)
{
completedWord = completedWord + wordArray[i] + ' ';
}
else
{
completedWord = completedWord + "\n" + wordArray[i] + ' ';
}
temp = "";
}
return completedWord;
}
一旦句子是37个字符,它就会继续else
。在添加\n
之前,我希望每一行都是37。只有当句子长于37个字符
答案 0 :(得分:3)
这应该可以解决问题。顺便说一下,我会使用StringBuilder来方便。
static string sentence(string statement)
{
if (statement.Length > 37)
{
var words = statement.Split(' ');
StringBuilder completedWord = new StringBuilder();
int charCount = 0;
if (words.Length > 1)
{
for (int i = 1; i < words.Length - 1; i++)
{
charCount += words[i].Length;
if (charCount >= 37)
{
completedWord.AppendLine();
charCount = 0;
}
completedWord.Append(words[i]);
completedWord.Append(" ");
}
}
// add the last word
if (completedWord.Length + words[words.Length - 1].Length >= 37)
{
completedWord.AppendLine();
}
completedWord.Append(words[words.Length - 1]);
return completedWord.ToString();
}
return statement;
}
答案 1 :(得分:2)
您可以像下面一样执行字符串子字符串方法,
nameID DIALOG x, y, width, height [optional-statements] {control-statement . . . }
答案 2 :(得分:1)
我用这个:
/// <summary>
/// Wrap lines in strings longer than maxLen by interplating new line
/// characters.
/// </summary>
/// <param name="lines">the lines to process</param>
/// <param name="maxLen">the maximum length of each line</param>
public static string[] wrap_lines(string[] lines, int maxLen)
{
List<string> output = new List<string>();
foreach (var line in lines)
{
var words = line.Split(' ');
string newWord = words[0] + " ";
int len = newWord.Length;
for (int i = 1; i < words.Length; i++)
{
if (len + words[i].Length + 1 > maxLen)
{
len = 0;
newWord += "\n";
i--;
}
else
{
len += words[i].Length + 1;
string ch = i == words.Length - 1 ? "" : " ";
newWord += words[i] + ch;
}
}
output.Add(newWord);
}
return output.ToArray();
}
它假设没有单词长于 maxLen
。
答案 3 :(得分:0)
更正了你的for循环:
for (int i = 0; i < wordArray.Length; i++)
{
//temp = completedWord + wordArray[i] + ' '; //remove it
temp = temp + wordArray[i] + ' '; //added
if (temp.Length < 37)
{
completedWord = completedWord + wordArray[i] + ' ';
}
else
{
completedWord = completedWord + "\n"; //corrected
temp = ""; //added
}
//temp = ""; //remove it
}
答案 4 :(得分:0)
您可以包含一个记录当前记录行数的字段:
string completedWord = "";
string temp = "";
string[] wordArray = statement.Split(' ');
int lines = 1;
for (int i = 0; i < wordArray.Length; i++)
{
temp = completedWord + wordArray[i] + ' ';
if (temp.Length < 37* lines)
{
completedWord = completedWord + wordArray[i] + ' ';
}
else
{
completedWord = completedWord + "\n" + wordArray[i] + ' ';
lines += 1;
}
temp = "";
}
答案 5 :(得分:0)
一旦你添加了第一个\ n,字符串总是超过37个字符,所以第一个if(len&lt; 37)测试只会返回true一次。
相反,你需要另一个变量,即
string tempLine = "";
然后当你迭代你的单词集合时,通过tempLine构建一个由总计&lt; = 37个字符组成的单词组成的行,一旦你达到最大值,将它添加到completedWord,然后在下一个循环之前重置tempLine =“”。 / p>
temp = tempLine + wordArray[i] + ' ';
if (temp.Length < 37)
{
tempLine = tempLine + wordArray[i] + ' ';
}
else
{
completedWord = completedWord + tempLine + "\n";
tempLine = "";
}
temp = "";
答案 6 :(得分:0)
这是我的努力。它是一个简短的小递归函数,它接受您希望分成多行的字符串,并将每行的最大长度(截止点)作为参数。
从开始到所需的行长,它都是输入文本的子字符串,并将其添加到“输出”变量中。然后,它将输入字符串的其余部分反馈回该函数,在该函数中,它继续递归调用自身,直到其余部分的长度小于所需的行长为止,然后返回输出变量。
希望如此。
deploy-heroku
答案 7 :(得分:0)
目前接受的答案过于复杂,不确定是否准确(见评论)。一个更简单准确的解决方案是:
public static class StringExtensions
{
public static string BreakLongLine(this string line, int maxLen, string newLineCharacter)
{
// if there is nothing to be split, return early
if (line.Length <= maxLen)
{
return line;
}
StringBuilder lineSplit = new StringBuilder();
var words = line.Split(' ');
var charCount = 0;
for (var i = 0; i < words.Length; i++)
{
if (charCount + words[i].Length >= maxLen)
{
// '>=' and not '>' because I need to add an extra character (space) before the word
// and last word character should not be cut
lineSplit.Append(newLineCharacter);
charCount = 0;
}
if (charCount > 0)
{
lineSplit.Append(' ');
charCount++;
}
lineSplit.Append(words[i]);
charCount += words[i].Length;
}
return lineSplit.ToString();
}
}
请注意此解决方案:
我也在单元测试中介绍了这个方法,所以你可以看到它是有效的:
public class StringExtensionsTests
{
[Fact]
public void SplitIntoTwoLines()
{
// arrange
const string longString = "Four words two lines";
// act
var resultString = longString.BreakLongLine(10, "\n");
// assert
Assert.Equal("Four words\ntwo lines", resultString);
}
[Fact]
public void SplitIntoThreeLines()
{
// arrange
const string longString = "Four words two lines";
// act
var resultString = longString.BreakLongLine(9, "\n");
// assert
Assert.Equal("Four\nwords two\nlines", resultString);
}
// https://stackoverflow.com/questions/15793409/how-to-split-a-string-into-multiple-lines-if-more-than-37-characters-are-present
[Fact]
public void StackOverflowExample()
{
// arrange
const string longString = "The Quick Brown Fox Jumped Over The Lazy Dog";
// act
var resultString = longString.BreakLongLine(37, "\n");
// assert
Assert.Equal("The Quick Brown Fox Jumped Over The\nLazy Dog", resultString);
}
}