C# 如何创建没有空格或空行的文本文件?只有一段文字

C# 如何创建没有空格或空行的文本文件?只有一段文字,c#,C#,这是我在form1构造函数中如何使用文本文件的方法: ww = new StreamWriter(@"c:\temp\test.txt"); 创建空文本文件: ww = new StreamWriter(@"c:\temp\test.txt"); 编码,因为它是希伯来语内容和下载: client.Encoding = System.Text.Encoding.GetEncoding(1255); page = client.DownloadString("http://rotter.net/

这是我在form1构造函数中如何使用文本文件的方法:

ww = new StreamWriter(@"c:\temp\test.txt");
创建空文本文件:

ww = new StreamWriter(@"c:\temp\test.txt");
编码,因为它是希伯来语内容和下载:

client.Encoding = System.Text.Encoding.GetEncoding(1255);
page = client.DownloadString("http://rotter.net/scoopscache.html");
客户端=网络客户端 页面=字符串

然后我从页面中提取日期、时间和文本:

TextExtractor.ExtractDateTime(page, newText, dateTime);
StreamWriter w = new StreamWriter(@"d:\rotterhtml\rotterscoops.html");
w.Write(page);
w.Close();
TextExtractor.ExtractText(@"d:\rotterhtml\rotterscoops.html", newText, dateTime);
然后我将带有空格的新内容写入文本文件test.txt:

combindedString = string.Join(Environment.NewLine, newText);
ww.Write(combindedString);
ww.Close();
CombinedString=字符串

这是TextExtractor类,我从页面中提取日期、时间和文本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace ScrollLabelTest
{
    class TextExtractor
    {
        public static void ExtractText(string filePath, List<string> newText, List<string> dateTime)
        {
            //newText = new List<string>();
            List<string> text = new List<string>();
            var htmlDoc = new HtmlAgilityPack.HtmlDocument();
            htmlDoc.OptionFixNestedTags = true;
            htmlDoc.Load(filePath, System.Text.Encoding.GetEncoding(65001));

            if (htmlDoc.DocumentNode != null)
            {
                var nodes = htmlDoc.DocumentNode.SelectNodes("//a/b");
                foreach (var node in nodes)
                {
                    text.Add(node.InnerText);
                }
            }
            List<string> t = filterNumbers(text);
            for (int i = 0; i < t.Count; i++)
            {
                newText.Add(t[i]);
                newText.Add(dateTime[i]);
                newText.Add("");
            }
        }

        public static void ExtractDateTime(string text, List<string> newText, List<string> dateTime)
        {

            //dateTime = new List<string>();
            string pattern1 = "<span style=color:#000099;>(?'hebrew'[^<]*)</span>";
            Regex expr1 = new Regex(pattern1, RegexOptions.Singleline);
            MatchCollection matches = expr1.Matches(text);
            foreach (Match match in matches)
            {
                string hebrew = match.Groups["hebrew"].Value;

                string pattern2 = @"[^\s$]*:[^:]*:\s+\d\d:\d\d";
                Regex expr2 = new Regex(pattern2);
                Match match2 = expr2.Match(hebrew);
                string results = match2.Value;
                int i = results.IndexOf("שעה");
                results = results.Insert(i + "שעה".Length, " ");
                dateTime.Add("דווח במקור " + results);
            }
        }

        private static List<string> filterNumbers(List<string> mix)
        {
            List<string> onlyStrings = new List<string>();
            foreach (var itemToCheck in mix)
            {
                int number = 0;
                if (!int.TryParse(itemToCheck, out number))
                {
                    onlyStrings.Add(itemToCheck);
                }
            }
            return onlyStrings;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Text.RegularExpressions;
名称空间滚动标签测试
{
类文本提取器
{
公共静态void ExtractText(字符串文件路径、列表新文本、列表日期时间)
{
//newText=新列表();
列表文本=新列表();
var htmlDoc=新的HtmlAgilityPack.HtmlDocument();
htmlDoc.OptionFixNestedTags=true;
Load(filePath,System.Text.Encoding.GetEncoding(65001));
if(htmlDoc.DocumentNode!=null)
{
var nodes=htmlDoc.DocumentNode.SelectNodes(“//a/b”);
foreach(节点中的var节点)
{
添加(node.InnerText);
}
}
列表t=过滤器编号(文本);
对于(int i=0;istring pattern1=“(?'hebrew'[^这将为您修复它:

using (StreamWriter sw = new StreamWriter(@"C:\temp\test1.txt", false))
{
     using (StreamReader sr = new StreamReader(@"C:\temp\test.txt"))
     {
          while (sr.Peek() >= 0)
          {
                 var strReadLine = sr.ReadLine().Trim().Replace("\t", "").Replace("\r\n", "");
                 if (!String.IsNullOrWhiteSpace(strReadLine)) 
                 {
                        sw.WriteLine(strReadLine);               
                 }
          }
     }    
}

您可以循环浏览文本文件的每一行,边走边修剪。修剪空格和回车符
\r
\n