如果CRLF出现在一行内,则用空格替换CRLF [英] Replacing CRLF with a blank space if it occurs within a line
问题描述
大家好,
我有一个简单的C#程序,它将某些十六进制值替换为CSV文件中的空白区域。
我替换十六进制值的方式如下:
string value =\ x00;
value = Regex.Escape(value);
stext = Regex.Replace(stext ,值,);
我的问题是CSV文件可能包含同一行/记录/行中的多个CRLF。
有没有办法在它不在行尾时更换CRLF?
示例:
字段1 CRLF,字段2 CRLF,CRLF
- 有没有办法摆脱前两个CRLF?
谢谢!
Hello everyone,
I have a simple C# program that replaces certain hex values to an empty space in CSV files.
The way I replace hex values is as follows:
string value = "\x00";
value = Regex.Escape(value);
stext = Regex.Replace(stext, value, "");
My problem is a CSV file may contain multiple CRLFs within the same row/record/line.
Is there a way to replace a CRLF only if it is not at the end of line?
Example:
"field 1 CRLF","field 2 CRLF",CRLF
-- is there a way to get rid of the first two CRLFs?
thanks!
推荐答案
从技术上讲,CRLF
会终止一条线,因此你不能在一条线内。
但是如果你有一种识别字段的独特方式,你可以在你的文件中删除''spurious''CRLF
。
例如,如果每条条信息必须包含两个字段,您可以清楚地从字段2中分离
,虚假的字段1
CRLF
实际上是不相关的,你有办法丢弃它们。
Technically aCRLF
terminates a line hence you cannot have it inside a line.
However you may remove ''spurious''CRLF
in you file if you have a unique way for identifying your fields.
For instance if every piece of info must contain two fields and you may clearly separatefield 1
fromfield 2
, the the spuriousCRLF
are in fact irrelevant and you have a way for discarding them.
As如果您正在读取该文件,则需要检查是否正在读取带引号的字符串内的字符(例如,指示您是否找到引号的布尔值),如果是,请保留某些字符,例如< cr>和字符串中的< lf> ;.
当你到达< cr>或者< lf>不在带引号的字符串中的字符,那么你可以说这是行/行/记录的结尾。然后字符串本身应该只包含你需要转换的字符。
我相信引号中的双引号表示引号,所以你也需要注意它。
问候,
Ian。
As you are reading the file in, you will need to check whether you are reading characters that are inside a quoted string (e.g. a boolean indicating whether you have found a quote mark) and if so, keep certain characters, such as <cr> and <lf>, in the string.
When you reach a <cr> or <lf> character that is not in a quoted string then you can say that is the end of line/row/record. The string itself should then only contain the characters which you need to convert.
I believe a quotation mark is indicated by a double quote within a quoted string, so you will need to pay attention for that also.
Regards,
Ian.
这篇关于如果CRLF出现在一行内,则用空格替换CRLF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!