How to print a double quote using a Zebra printer with EPL? - java

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?

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]

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);

Related

How to replace single quote within a string with two single quotes in java?

I have the below data in my text file.
1|"John"|3,5400
2|"Jim"|7,7300
3|"Smith,Robin",3,4300
4|"O'Brien",10,8200
and I want this output:
(1,'John',3,5400)
(2,'Jim',7,7300)
(3,'Smith,Robin',3,4300)
(4,'O''Brien',10,8200)
Basically I want to replace | character with commas and double quotes with single quote. I am able to achieve that with this piece of code:
String text2 = textAfterHeader.replaceAll("\\|", ",").replaceAll("\"", "'").replaceAll("[a-zA-Z]'[a-zA-Z]", "''");
output that I am getting:
1,'John',3,5400
2,'Jim',7,7300
3,'Smith,Robin',3,4300
4,'''rien',10,8200
But I have one more requirement where I need to put two single quotes whenever a single quote appears between a string, for example, O'Brien as O''Brien. But this part is not working.
As was suggested by #AndyTurner, you can simplify the problem by first replacing all ' with '' and then replace all " with '. The only thing missing after that are the parenthesis, which can be added in two steps:
Replace all blanks with ) ( (notice the blank between the parenthesis).
Add a leading ( and a trailing ) to the String.
All together, a solution could look like this:
final String output = "("
+ input
.replace("'", "''")
.replace("\"", "'")
.replace("|", ",")
.replace(" ", ") (")
+ ")";
Ideone demo
Try this regex:
\s*\'\s*
and a call to Replace with " will do the job.
A possible solution could be:
String lineSeparator = System.getProperty("line.separator");
String output = new StringBuilder("(").append(
input.replaceAll("\\|", ",")
.replaceAll("'", "''") // (*)
.replaceAll("\"", "'") // (**)
.replaceAll(lineSeparator, ")" + lineSeparator + "("))
.append(")").toString();
Note the replacement (*) must come before (**). Since you need to replace exact characters instead of variable regular expressions, you want better use replace instead of replaceAll.

How to prevent Html.fromHtml from trimming whitespace

In an Android Java project, I have a string like this one (although with varying amounts of whitespace on either side):
String foo = " foo bar "
The whitespace on the two sides of the string is important, as the actual string contains indented code with HTML syntax highlighting.
When I pass the string through Html.fromHtml, this start and end whitespace is removed, but I need to keep the whitespace there:
Html.fromHtml(foo).toString() // "foo bar" - I want " foo bar "
How I can preserve the whitespace on the sides of the string through the Html.fromHtml call?
Try Using TextUtils.htmlEncode(str).
This method will escape all html string character.
https://developer.android.com/reference/android/text/TextUtils.html#htmlEncode(java.lang.String)
Yazan's suggestion was sufficient,but since you said that the string is generated dynamically,you can always take the newly generated string s for example and use the concat() method.For example s.concat(" &nbsp");
As Html.fromHtml() parses the string as html tags and content may be you want to use the encoded character for space which is
try this in your code
String foo = " foo bar ";
Note: repeat as many spaces as you need to show.
Edit:
if you are getting your string from somewhere else, you can replace spaces with before passing it
String foo = getMyFoo();
foo = foo.replaceAll(" "," ");
For preserving starting whitespace, this Kotlin code appears to work, and probably wouldn't be difficult to adapt for working with ending whitespace either:
fun replaceWithNonBreakingAtStart(str: String) = (1..(str.takeWhile { it == ' ' }.count())).map { " " }.joinToString("") + str.trimStart()

How to replace " with blank in a string in java?

I have this string "Distrik Timur". I want to remove " from this string.
I thought String value = translatedValue.replace(""","") will work.But its not working.
Quotation marks need to be escaped: write translatedValue.replace("\"", "").

Remove curly brace in Java

I have a text file in which each line begins and ends with a curly brace:
{aaa,":"bbb,ID":"ccc,}
{zzz,":"sss,ID":"fff,}
{ggg,":"hhh,ID":"kkk,} ...
Between the characters there are no spaces. I'm trying to remove the curly braces and replace them with white space as follows:
String s = "{aaa,":"bbb,ID":"ccc,}";
String n = s.replaceAll("{", " ");
I've tried escaping the curly brace using:
String n = s.replaceAll("/{", " ");
String n = s.replaceAll("'{'", " ");
None of this works, as it comes up with an error. Does anyone know a solution?
you cannot define a String like this:
String s = "{aaa,":"bbb,ID":"ccc,}";
The error is here, you have to escape the double quotes inside the string, like this:
String s = "{aaa,\":\"bbb,ID\":\"ccc,}";
Now there will be no error if you call
s.replaceAll("\\{", " ");
If you have an IDE (that is a program like eclipse), you will notice that a string is colored different from the standard color black (for example the color of a method or a semicolon [;]). If the string is all of the same color (usually brown, sometimes blue) then you should be ok, if you notice some black color inside, you are doing something wrong. Usually the only thing that you would put after a double quote ["] is a plus [+] followed by something that has to be added to the string. For example:
String firstPiece = "This is a ";
// this is ok:
String all = s + "String";
//if you call:
System.out.println(all);
//the output will be: This is a String
// this is not ok:
String allWrong = s "String";
//Even if you are putting one after the other the two strings, this is forbidden and is a Syntax error.
String.replaceAll() takes a regex, and regex requires escaping of the '{' character. So, replace:
s.replaceAll("{", " ");
with:
s.replaceAll("\\{", " ");
Note the double-escapes - one for the Java string, and one for the regex.
However, you don't really need a regex here since you're just matching a single character. So you could use the replace method instead:
s.replace("{", " "); // Replace string occurrences
s.replace('{', ' '); // Replace character occurrences
Or, use the regex version to replace both braces in one fell swoop:
s.replaceAll("[{}]", " ");
No escaping is needed here since the braces are inside a character class ([]).
Just adding to the answer above:
If somebody is trying like below, this won't work:
if(values.contains("\\{")){
values = values.replaceAll("\\{", "");
}
if(values.contains("\\}")){
values = values.replaceAll("\\}", "");
}
Use below code if you are using contains():
if(values.contains("{")){
values = values.replaceAll("\\{", "");
}
if(values.contains("}")){
values = values.replaceAll("\\}", "");
}

'\' Getting Stripped Automatically In StringObject.ReplaceAll method in Java

I am using Jre 1.6.
I am executing the following lines of code:
String unicodeValue = "\u001B"; text = text.replaceAll("" + character, unicodeValue);
Here, text is a string object containing an invalid XML character of Unicode value '\u001B'.
So, I am converting the invalid XML character to its Unicode value to write in the XML.
But on doing text.replaceAll, the '\' is getting stripped and the character is replaced by 'u001B'.
Can anyone please suggest a way to retain the '\' after replacing the character with its unicode value ?
The problem is that str.replaceAll(regex, repl) is defined as returning the same as
Pattern.compile(regex).matcher(str).replaceAll(repl)
But the documentation for replaceAll says,
Note that backslashes () and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.
So this means we need to add several extra layers of escaping:
public class Foo {
public static void main(String[] args)
{
String unicodeValue = "\u001B";
String escapedUnicodevalue = "\\\\u001B";
String text = "invalid" + unicodeValue + "string";
text = text.replaceAll(unicodeValue, escapedUnicodevalue);
System.out.println(text);
}
}
prints invalid\u001Bstring as desired.
Use double slash \\ to represent escaped \:
String unicodeValue = "\\u001B"; text = text.replaceAll("" + character, unicodeValue);
This ran perfect. I tested it.
char character = 0x1b;
String unicodeValue = "\\\\u001B";
String text = "invalid " + character + " string";
System.out.println(text);
text = text.replaceAll("" + character, unicodeValue);
System.out.println(text);
Just used a concept of RegEx.

Categories

Resources