简体   繁体   English

删除双反斜杠C#(用于ESC / POS编程)

[英]Remove double backslashes c# (for use ESC/POS programming)

I've seached a long time, and it seems that my problem is world-wide known. 我已经待了很长时间了,看来我的问题已广为人知。 But, all the answers that are given, won't work for me. 但是,所有给出的答案对我都行不通。 Most of the time, people say 'there is no problem'. 大多数时候,人们说“没有问题”。

The problem: I'm programming a POS solution, and I'm using a Epson POS printer. 问题:我正在编程POS解决方案,并且正在使用Epson POS打印机。 To print the buttom to the receipt, I'm storing a string in the database. 要将臀部打印到收据上,我正在数据库中存储一个字符串。 This is, so users can adjust the text at the bottom of the receipt. 这样,用户就可以调整收据底部的文本。 But, when I'm pulling the string out of the database, C# adds slashes to the string, so my excape characters won't work. 但是,当我从数据库中提取字符串时,C#在字符串中添加了斜杠,因此我的排字字符将无法工作。 I know, that usualy is not a problem, but in my case it is, because my ECS/POS commands won't work. 我知道,这通常不是问题,但就我而言,是这样,因为我的ECS / POS命令不起作用。

I've already tried some scripts, which replaces the double \\ with a single \\, but they don't work. 我已经尝试过一些脚本,用单\\代替双\\,但是它们不起作用。 (eg. String.Replace(@'\\\\',@'\\'). (例如String.Replace(@'\\\\',@'\\')。

Problem: 问题:

I have a sting: "foo \\n bar" 我有一个刺:“ foo \\ n bar”

Needs to print as: 需要打印为:

foo

bar 酒吧

C# adds slashes: "foo \\\\n bar" C#添加斜杠:“ foo \\\\ n bar”

Now it's printed as: foo \\n bar 现在将其打印为:foo \\ n bar

Anyone an idea? 有人知道吗?

The problem is a misunderstanding of how C# handles strings. 问题是对C#如何处理字符串的误解。 Take the following sample code: 采取以下示例代码:

string foo = "a\nb";
int fooLength = foo.Length; \\ 3 characters.
int bar = (int)(foo[1]); \\ 10 = linefeed character.

versus: 与:

string foo = @"a\nb"; \\ NB: @ prefix!
int fooLength = foo.Length; \\ 4 characters.
int bar = (int)(foo[1]); \\ 92 = backslash character.

The first example uses a string literal ( "a\\nb" ) which is interpreted by the C# compiler to yield three characters. 第一个示例使用字符串文字( "a\\nb" ),C#编译器将其解释为产生三个字符。 The second example uses a verbatim string literal , due the prefix @, that suppresses the interpretation of the string. 第二个示例使用前缀@的逐字字符串文字 ,它禁止对该字符串进行解释。

Note that the debugger is designed to add to the confusion by displaying strings with escape codes added, eg string foo = "a\\nb" + (Char)9; 请注意,调试器旨在通过显示添加了转义码的字符串来增加混乱,例如: string foo = "a\\nb" + (Char)9; results in a string that the debugger shows as "a\\nb\\t" . 导致调试器显示为"a\\nb\\t"的字符串。 If you use the "text visualizer" in the debugger (by clicking on the magnifying glass when examining the the variable's value) you can see the difference between literal and interpreted characters. 如果在调试器中使用“文本可视化器”(在检查变量的值时单击放大镜),则可以看到文字字符和解释字符之间的区别。

Databases are, as a rule, designed to accept and return string values without interpretation. 通常,数据库旨在不经解释地接受和返回字符串值。 That way you needn't worry about names like "Delete D'table". 这样,您就不必担心“ Delete D'table”之类的名称。 Neither the presence of a SQL keyword, nor punctuation used in SQL statements, should present a problem in a data column. SQL关键字的存在或SQL语句中使用的标点都不应该在data列中出现问题。

Now the OP's issue should be becoming clearer. 现在,OP的问题应该变得更加清晰。 The string retrieved from the database does not contain a linefeed, but instead contains the characters '\\' and 'n'. 从数据库检索的字符串不包含换行符,而是包含字符“ \\”和“ n”。 .NET has no reason to change those values when the string is read from the database and written to a printer. 从数据库读取字符串并将其写入打印机时,.NET没有理由更改这些值。 Unfortunately, the debugger confounds the difference. 不幸的是,调试器混淆了两者之间的差异。 (Use the text visualizer as described above.) (如上所述,使用文本可视化器。)

The solution involves adding code to reproduce the C# compiler's processing of escape sequences. 该解决方案涉及添加代码以重现C#编译器对转义序列的处理。 (This should include escaping escape characters!) Alternatively, tokens can be added that are suitable for the application at hand, eg occurrences of «ESC» could be replaced with an ASCII escape character. (这应该包括转义的转义字符!)或者,可以添加适合于当前应用程序的令牌,例如,可以用ASCII转义字符替换出现的«ESC» This can be employed for longer sequences, for example if a print uses several characters to introduce a font change then write the code to replace «SetFont» with the correct sequence. 这可以用于更长的序列,例如,如果印刷品使用多个字符进行字体更改,然后编写代码以正确的序列替换«SetFont» More generally, you can replace a snippet with a dynamic value, eg «Now» could be replaced with the current date/time when the receipt is being printed. 通常,您可以用动态值替换代码段 ,例如,在打印收据时,可以将«Now»替换为当前日期/时间。 (Register number, cashier name, store hours, ... .) This makes the values in the database more human readable than embedded Unicode oddities and more flexible than fixed strings. (寄存器号,收银员姓名,营业时间等。)这使得数据库中的值比嵌入式Unicode奇数更易于阅读,比固定字符串更灵活。

Left as an exercise for the reader: extend snippets to support formatting and null value substitution. 留给读者练习:扩展代码片段以支持格式设置和空值替换。 «Now|DD.MM.YY hh:mm» to specify a format, «Discount|*|n/a» to specify a value ("n/a") to be displayed if the field is null. «Now|DD.MM.YY hh:mm»指定格式, «Discount|*|n/a»指定字段为空时要显示的值(“ n / a”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM