3

In Java, I want to print a label with a String as the input:

 String command
            = "N\n"
            + "A50,5,0,1,2,2,N,\"" + name + "\"\
            + "P1\n";    

But when the input (name) has a double quote character ("), it is blank and prints nothing. I have tried using the replace function:

name.replace('"', '\u0022');          

but it doesn't work. I want that double quote printed in label, how can I do this?

Undo
  • 25,519
  • 37
  • 106
  • 129
wanz
  • 302
  • 1
  • 5
  • 17

2 Answers2

3

Sending the " character in the text field of the EPL string makes the EPL code think it is the end of the string you are trying to print.

So, if you want to send(and print) "hello" you have to put a backslash before each " character and send \"hello\"

You also have to do that for backslashes.

So, your (EPL)output to the printer would have quotes to begin and end the string, and \" to print the quote characters WITHIN the string :

A30,210,0,4,1,1,N,"\"hello\""\n

Also remember you have to escape to characters to build a c# string so in c# it would look like this: outputEPLStr += "A30,210,0,4,1,1,N,\"\\"hello\\"\"\n";

[which contains 6 escaped characters]

  • also remember to process for any \ characters FIRST before you process any " characters, so you don't process out your \ chars name = name.replace("\\", "\\\\"); name = name.replace("\"", "\\""); String command = "N\n" + "A50,5,0,1,2,2,N,\"" + name + "\"" + "P1\n"; System.out.println(command); – Michael Kosak May 03 '18 at 14:36
2

Couple of points:

  • replace method returns back string after replacing so you should expect something like:

    command = command.replace...
    
  • quote has special meaning and hence needs to be escaped in Java. You need the following:

    name = name.replace("\"", "");
    String command
        = "N\n"
        + "A50,5,0,1,2,2,N,\"" + name + "\""
        + "P1\n"; 
    System.out.println(command);
    
SMA
  • 36,381
  • 8
  • 49
  • 73