[英]How do i create the text file to be without spaces or empty lines ? Just one block of text
This is how i use in form1 constructor with the text file: 这就是我在文本文件的form1构造函数中使用的方式:
Create empty text file: 创建空的文本文件:
ww = new StreamWriter(@"c:\temp\test.txt");
Encoding since it's hebrew the content and downloading it: 编码,因为它希伯来语内容并下载:
client.Encoding = System.Text.Encoding.GetEncoding(1255);
page = client.DownloadString("http://rotter.net/scoopscache.html");
client = WebClient page = string 客户端= WebClient页面=字符串
Then i extract the date and time and the text from the page: 然后我从页面中提取日期和时间以及文本:
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);
Then i write the new content with the spaces to a text file test.txt: 然后,我将带有空格的新内容写入文本文件test.txt:
combindedString = string.Join(Environment.NewLine, newText);
ww.Write(combindedString);
ww.Close();
combindedString = string combindedString =字符串
And this is the class TextExtractor where i extract the date and time and text from page: 这是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;
}
}
}
And this is the text file test.txt in the end after all the extraction: 这是所有提取之后的文本文件test.txt:
You can see the first line is empty line then the fiest text line not beging from the left first left side but there is a space from the left side. 您可以看到第一行是空行,然后最细的文本行不是从左到左,从左开始是空白。 Then between each two lines there is a space/empty line. 然后在每两行之间有一个空格/空行。
What i want is that the text file will be without any space not from any line beginning and not between any line/s and that in the top there will be no first empty line. 我想要的是文本文件将没有任何空间,不是从任何行开始也不在任何行之间,并且在顶部将没有第一个空行。
Just one block of text. 仅一小段文字。
This will fix it for you: 这将为您解决:
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);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.