添加到字符串直到达到长度(noobie C#guy)
我试图读取一个文本文件,将其分解成一个字符串数组,然后从单词中编译出新字符串,但我不希望它的长度超过120个字符 我所做的是让它写PML为我使用的一些软件创建一个宏,并且文本不能超过120个字符。为了更进一步,我需要将120个字符或更少的字符串(最接近的单词)包装为“BTEXT |这里的字符串|”,这是命令 代码如下:添加到字符串直到达到长度(noobie C#guy),c#,string,stringbuilder,C#,String,Stringbuilder,我试图读取一个文本文件,将其分解成一个字符串数组,然后从单词中编译出新字符串,但我不希望它的长度超过120个字符 我所做的是让它写PML为我使用的一些软件创建一个宏,并且文本不能超过120个字符。为了更进一步,我需要将120个字符或更少的字符串(最接近的单词)包装为“BTEXT |这里的字符串|”,这是命令 代码如下: static void Main(string[] args) { int BIGSTRINGLEN = 120; string readit = File
static void Main(string[] args)
{
int BIGSTRINGLEN = 120;
string readit = File.ReadAllText("C:\\stringtest.txt");
string finish = readit.Replace("\r\n", " ").Replace("\t", "");
string[] seeit = finish.Split(' ');
StringBuilder builder = new StringBuilder(BIGSTRINGLEN);
foreach(string word in seeit)
{
while (builder.Length + " " + word.Length <= BIGSTRINGLEN)
{
builder.Append(word)
}
}
}
static void Main(字符串[]args)
{
int-BIGSTRINGLEN=120;
string readit=File.ReadAllText(“C:\\stringtest.txt”);
string finish=readit.Replace(“\r\n”,”).Replace(“\t”,”);
字符串[]seeit=finish.Split(“”);
StringBuilder=新的StringBuilder(BIGSTRINGLEN);
foreach(seeit中的字符串字)
{
而(builder.Length+“”+word.Length尝试使用if而不是while,因为如果不是,您将不断追加同一个单词!!尝试使用if而不是while,因为如果不是,您将不断追加同一个单词!!而不是将整个文件读入内存,您可以一次读取一行。这将减少内存需求,还可以防止你不必换新词吗
StringBuilder builder = new StringBuilder(BIGSTRINGLEN);
foreach (var line in File.ReadLines(filename))
{
// clean up the line.
// Do you really want to replace tabs with nothing?
// if you want to treat tabs like spaces, change the call to Split
// and include '\t' in the character array.
string finish = line.Replace("\t", string.Empty);
string[] seeit = finish.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in seeit)
{
if ((builder.Length + word.Length + 1 <= BIGSTRINGLEN)
{
if (builder.Length != 0)
builder.Append(' ');
builder.Append(word);
}
else
{
// output line
Console.WriteLine(builder.ToString());
// and reset the builder
builder.Length = 0;
}
}
}
// and write the last line
if (builder.Length > 0)
Console.WriteLine(builder.ToString());
StringBuilder=新的StringBuilder(BIGSTRINGLEN);
foreach(文件中的var行。ReadLines(文件名))
{
//把线清理干净。
//是否确实要将选项卡替换为空?
//如果要将选项卡视为空格,请将调用更改为Split
//并在字符数组中包含“\t”。
字符串结束=行。替换(“\t”,字符串。空);
字符串[]seeit=finish.Split(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);
foreach(seeit中的字符串字)
{
如果((builder.Length+word.Length+1 0)
Console.WriteLine(builder.ToString());
如果一个单词的长度超过BIGSTRINGLEN
,则该代码将失败。长单词最终将输出一个空行。我认为,如果出现问题,您可以找出如何处理该情况。您可以一次读取一行,而不是将整个文件读入内存。这将减少内存需求,并防止你不必换新词吗
StringBuilder builder = new StringBuilder(BIGSTRINGLEN);
foreach (var line in File.ReadLines(filename))
{
// clean up the line.
// Do you really want to replace tabs with nothing?
// if you want to treat tabs like spaces, change the call to Split
// and include '\t' in the character array.
string finish = line.Replace("\t", string.Empty);
string[] seeit = finish.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in seeit)
{
if ((builder.Length + word.Length + 1 <= BIGSTRINGLEN)
{
if (builder.Length != 0)
builder.Append(' ');
builder.Append(word);
}
else
{
// output line
Console.WriteLine(builder.ToString());
// and reset the builder
builder.Length = 0;
}
}
}
// and write the last line
if (builder.Length > 0)
Console.WriteLine(builder.ToString());
StringBuilder=新的StringBuilder(BIGSTRINGLEN);
foreach(文件中的var行。ReadLines(文件名))
{
//把线清理干净。
//是否确实要将选项卡替换为空?
//如果要将选项卡视为空格,请将调用更改为Split
//并在字符数组中包含“\t”。
字符串结束=行。替换(“\t”,字符串。空);
字符串[]seeit=finish.Split(新字符[]{''},StringSplitOptions.RemoveEmptyEntries);
foreach(seeit中的字符串字)
{
如果((builder.Length+word.Length+1 0)
Console.WriteLine(builder.ToString());
如果一个单词的长度超过BIGSTRINGLEN
,那么该代码将失败。长单词最终将输出一个空行。我认为如果出现问题,您可以找出如何处理该情况。Matthew Moon是对的-您的while
循环将无法按当前位置工作
但除此之外,你在这方面有一些问题
while (builder.Length + " " + word.Length <= BIGSTRINGLEN)
Matthew Moon是对的-您的while
循环不会像当前放置的那样工作
但除此之外,你在这方面有一些问题
while (builder.Length + " " + word.Length <= BIGSTRINGLEN)
while行(while(builder.Length+“”+word.Length)要使while行工作,它可能应该类似于:while(builder.Length+1+word.Length)while行(while(builder.Length+“”+word.Length)要使while行工作,它可能应该类似于:while(builder.Length+1+word.Length)