C# 读一个文本文件,用1000个字符的块来写文本,并在C语言中保持单词的完整性#

C# 读一个文本文件,用1000个字符的块来写文本,并在C语言中保持单词的完整性#,c#,C#,我试图读取一个文件,并在每1000个字符后拆分文本。但我想保持文字完整。所以它应该在空间上分开。如果第1000个字符不是空格,则在其前面或后面的第一个空格处拆分。你知道怎么做吗?我还删除了文本中多余的空格 while ((line = file.ReadLine()) != null) { text = text + line.Trim(); noSpaceText = Regex.Replace

我试图读取一个文件,并在每1000个字符后拆分文本。但我想保持文字完整。所以它应该在空间上分开。如果第1000个字符不是空格,则在其前面或后面的第一个空格处拆分。你知道怎么做吗?我还删除了文本中多余的空格

                 while ((line = file.ReadLine()) != null)
        {

            text = text + line.Trim();
            noSpaceText = Regex.Replace(text, @"\r\n?|\n/", "");      

        }

        List<string> rowsToInsert = new List<string>();

        int splitAt = 1000; 
        for (int i = 0; i < noSpaceText.Length; i = i + splitAt)
        {
                if (noSpaceText.Length - i >= splitAt)
            {
                rowsToInsert.Add(noSpaceText.Substring(i, splitAt));
            }
            else
                    rowsToInsert.Add(noSpaceText.Substring(i, 
            ((noSpaceText.Length - i))));
        }

        foreach(var item in rowsToInsert)
          {
            Console.WriteLine(item);
          }
while((line=file.ReadLine())!=null)
{
text=text+line.Trim();
noSpaceText=Regex.Replace(text,@“\r\n?|\n/”,“”);
}
List rowsToInsert=新列表();
int splitAt=1000;
for(int i=0;i=splitAt)
{
rowsToInsert.Add(noSpaceText.Substring(i,splitAt));
}
其他的
rowsToInsert.Add(noSpaceText.Substring(i,
((noSpaceText.Length-i));
}
foreach(行中的var项插入)
{
控制台写入线(项目);
}

好的,只需键入此未经测试的解决方案即可:

public static List<string> SplitOn(this string input, int charLength, char[] seperator)
        {
            List<string> splits = new List<string>();
            var tokens = input.Split(seperator);
            // -1 because first token adds 1 to length
            int totalLength = -1;
            List<string> segments = new List<string>;
            foreach(var t in tokens)
            {
                if(totalLength + t.Length+1 > charLength)
                {
                    splits.Add(String.Join(" ", segments));
                    totalLength = -1;
                    segments.Clear();
                }
                totalLength += t.Length + 1;
                segments.Add(t);
            }
            if(segments.Count>0)
            {
                splits.Add(String.Join(" ", segments));
            }
            return splits;
        }
公共静态列表拆分(此字符串输入,int charLength,char[]分隔符)
{
列表拆分=新列表();
var标记=输入。拆分(分隔符);
//-1,因为第一个令牌将长度增加1
整数总长度=-1;
列表段=新列表;
foreach(令牌中的var t)
{
如果(总长度+t.长度+1>字符长度)
{
splits.Add(String.Join(“,segments));
总长度=-1;
段。清除();
}
总长度+=t.长度+1;
增加(t);
}
如果(段数>0)
{
splits.Add(String.Join(“,segments));
}
返回分裂;
}
这是一个扩展方法,它通过空格将输入文本分割成段,也就是说,我只使用单词在数组上迭代。然后计算每个段的长度,检查总长度并将其添加到结果列表中。

另一种解决方案:

public static List<string> SplitString(string stringInput, int blockLength)
{
    var output = new List<string>();
    var count = 0;
    while(count < stringInput.Length)
    {
        string block = "";
        if(count + blockLength > stringInput.Length)
        {
            block = stringInput.Substring(count, stringInput.Length - count);   
        }
        else
        {
            block = stringInput.Substring(count, blockLength + 1);  
        }

        if(block.Length < blockLength)
        {
            output.Add(block);  
            count += block.Length;
        }
        else if(block.EndsWith(" "))
        {
            output.Add(block);
            count = count+blockLength + 1;
        }
        else
        {   
            output.Add(block.Substring(0, block.LastIndexOf(" ")));
            count = count + block.LastIndexOf(" ") +1;
        }
    }

    return output;
}
公共静态列表拆分字符串(string stringInput,int blockLength)
{
var输出=新列表();
var计数=0;
while(计数stringInput.Length)
{
block=stringInput.Substring(count,stringInput.Length-count);
}
其他的
{
block=stringInput.Substring(计数,blockLength+1);
}
if(block.Length
使用子字符串从字符串中获取前1001个字符。检查最后一个字符是否为空格。如果是,则将其写入文本文件。如果不是,则从1001字符串的开始到最后一个空格字符读取子字符串,并将其写入文本文件。你就是这么做的。你能分享到目前为止你写的代码吗?然后解释你面临的问题是什么?你可以分割文本并迭代该数组,然后在每次分割之前或之后计算长度+1,你需要首先确定你需要做什么。@ChetanRanpariya,我正在努力检查那里的空格。很高兴我能帮上忙。