从文件中读取无特殊字符 [英] Read from file without special characters

查看:114
本文介绍了从文件中读取无特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用一个StreamReader打开一个文本文件,并抓住它的内容。我需要抓住只是从文件中的文本没有任何转义字符(\ n,即\ r \,等等)。谷歌是没有我现在。任何想法?

Im using a StreamReader to open a text file and grab its contents. I need to grab just the text from the file without any escape characters ( \n, \r, \", etc ). Google is failing me right now. Any ideas?

推荐答案

有在从文件中读取文本没有转义字符。转义字符,当你写一个字符串中使用,例如在程序code。我假设你的意思是要与普通的空格替换任何写入空格字符。

There are no escape characters in a text that you read from a file. Escape characters are used when you write a string literal, for example in program code. I assume that you mean that you want to replace any write space characters with plain spaces.

您可以使用常规的前pression匹配空格字符,用空格代替。它更容易使用 File.ReadAllText 来读取文件中的文本:

You can use a regular expression to match white space characters and replace them with spaces. It's easier to use the File.ReadAllText to read the text from the file:

string text = Regex.Replace(File.ReadAllText(fileName), @"[\r\n\t ]+", " ");

这篇关于从文件中读取无特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆