How to avoid backslash in java - java

First look at the code below
public static void main(String args[])
{
System.out.println(new Stringtest().test("The system has saved your payment under transaction number \369825655."));
}
private String test(String aa)
{
return aa.substring(58);
}
so logically this method should print 369825665. But it is printing 9825655 because of that backslash. Now I want the whole number. What should I do. I can not change the \ to \\ because the text is coming from a website.

\ is an escape character. You need to escape the escape character.
System.out.println(new Stringtest().test("The system has saved your payment under transaction number \\369825655."));
For more on escape sequences, see this Oracle document. Essentially the backslash tells the system that the character(s) after it should be interpreted in a special manner (not simply as plain text). When you escape the escape, it gets treated specially as well... as plain text, instead of as an escape character.
It can get confusing sometimes but is intuitive once you learn the core concepts.
EDIT: If you can't control the format the String comes to you in, you might be out of luck. I've been debugging this in Eclipse, and it seems like as soon as you create your String with that, the escape character gets processed and you lose the first two digits of your transaction number. You may need to get your database guys (or whomever formatted this terrible String) to change their implementation for you to do what you need to do. The Eclipse debugger suggests this, at least.
It just so happens that, apparently, \36 processes fine and gets interpreted as another ASCII character that doesn't show up. But in other cases, this will likely throw an Exception as an invalid escape sequence.
In my own testing, it seems that as soon as the String literal is declared/created, the loss of information occurs. So there will be no way to recover it after that to my knowledge.
Debug Screenshot

Try \\369825655 - basically it escapes the escape sign. Then replace all \ characters with empty characters.

Add another backslash (escape character)
ArrayList System.out.println(new Stringtest().test("The system has saved your payment under transaction number \\369825655."));

Related

JSCH Library: Getting strange character while reading readLine() [duplicate]

I am working with automation and using Jsch to connect to remote boxes and automate some tasks.
I am having problem parsing the command results because sometimes they come with ANSI Control chars.
I've already saw this answer and this other one but it does not provide any library to do that. I don't want to reinvent the wheel, if there is any. And I don't feel confident with those answers.
Right now, I am trying this, but I am not really sure it's complete enough.
reply = reply.replaceAll("\\[..;..[m]|\\[.{0,2}[m]|\\(Page \\d+\\)|\u001B\\[[K]|\u001B|\u000F", "");
How to remove ANSI control chars (VT100) from a Java String?
Most ANSI VT100 sequences have the format ESC [, optionally followed by a number or by two numbers separated by ;, followed by some character that is not a digit or ;. So something like
reply = reply.replaceAll("\u001B\\[[\\d;]*[^\\d;]","");
or
reply = reply.replaceAll("\\e\\[[\\d;]*[^\\d;]",""); // \e matches escape character
should catch most of them, I think. There may be other cases that you could add individually. (I have not tested this.)
Some of the alternatives in the regex you posted start with \\[, rather than the escape character, which may mean that you could be deleting some text you're not supposed to delete, or deleting part of a control sequence but leaving the ESC character in.

Safe sending String argument to JavaScript function from Java

My Java project based on WebView component.
Now, I want to call some JS function with single String argument.
To do this, I'm using simple code:
webEngine.executeScript("myFunc('" + str + "');");
*str text is getting from the texarea.
This solution works, but not safe enough.
Some times we can get netscape.javascript.JSException: SyntaxError: Unexpected EOF
So, how to handle str to avoid Exception?
Letfar's answer will work in most cases, but not all, and if you're doing this for security reasons, it's not sufficient. First, backslashes need to be escaped as well. Second, the line.separator property is the server side's EOL, which will only coincidentally be the same as the client side's, and you're already escaping the two possibilities, so the second line isn't necessary.
That all being said, there's no guarantee that some other control or non-ASCII character won't give some browser problems (for example, see the current Chrome nul in a URL bug), and browsers that don't recognize JavaScript (think things like screenreaders and other accessibility tools) might try to interpret HTML special characters as well, so I normally escape [^ -~] and [\'"&<>] (those are regular expression character ranges meaning all characters not between space and tilde inclusive; and backslash, single quote, double quote, ampersand, less than, greater than). Paranoid? A bit, but if str is a user entered string (or is calculated from a user entered string), you need to be a bit paranoid to avoid a security vulnerability.
Of course the real answer is to use some open source package to do the escaping, written by someone who knows security, or to use a framework that does it for you.
I have found this quick fix:
str = str.replace("'", "\\'");
str = str.replace(System.getProperty("line.separator"), "\\n");
str = str.replace("\n", "\\n");
str = str.replace("\r", "\\n");

Play a file after calling getPath in Java

The return value of a value from getPath() from the File Class is something like this
"C:\Users\Daniel\Desktop\ASDF.mp3".
To use the Desktop class from java to play a file, the path would have to be fed into a file, with a path similar to
"C:\\Users\\Daniel\\Desktop\\ASDF.mp3"
Since the \ is a reserved character(From my understanding) to make a new file you must use a double backslash to dictate that it is a file. My problem is that when I try to get the path I need to transform it into a double slash version. The .replaceAll() method doesn't allow for '\' since it's a reserved character but the .replace() method does.
To work around this would I just have to loop through to find all instances and replace them one at a time? Or is there a simpler work around? Also I would like to know if I am receiving this error due to it being a reserved character, or if I am completely wrong.
The two strings above are actually exactly the same.
When Java or other language outputs a string it only displays one slash '\', but when you are typing the string into double quotes you need double backslash '\\' so the Java parser knows it's one slash. Backslash is used for many other escape characters, so this is only way parser will know.
(Even when typing this answer, I needed 4 backslashes to make only 2!

How to remove ANSI control chars (VT100) from a Java String

I am working with automation and using Jsch to connect to remote boxes and automate some tasks.
I am having problem parsing the command results because sometimes they come with ANSI Control chars.
I've already saw this answer and this other one but it does not provide any library to do that. I don't want to reinvent the wheel, if there is any. And I don't feel confident with those answers.
Right now, I am trying this, but I am not really sure it's complete enough.
reply = reply.replaceAll("\\[..;..[m]|\\[.{0,2}[m]|\\(Page \\d+\\)|\u001B\\[[K]|\u001B|\u000F", "");
How to remove ANSI control chars (VT100) from a Java String?
Most ANSI VT100 sequences have the format ESC [, optionally followed by a number or by two numbers separated by ;, followed by some character that is not a digit or ;. So something like
reply = reply.replaceAll("\u001B\\[[\\d;]*[^\\d;]","");
or
reply = reply.replaceAll("\\e\\[[\\d;]*[^\\d;]",""); // \e matches escape character
should catch most of them, I think. There may be other cases that you could add individually. (I have not tested this.)
Some of the alternatives in the regex you posted start with \\[, rather than the escape character, which may mean that you could be deleting some text you're not supposed to delete, or deleting part of a control sequence but leaving the ESC character in.

Java String#contains() using String#matches() with escape character

I need a simple way to implement the contains function using matches. I believe this is my starting point:
xxx.matches("'.*yyy.*'");
But I need to make it a universal method and pre-process whatever I search for to be accepted by matches! This must be done using only the escape '\' character!
Imagine a string SEARCH_FOR that can contain some special characters that must be "regex escaped"...
String SEARCH_FOR="*.\\"
xxx.matches("'.*" + SEARCH_FOR + ".*'");
Are there any catches? Special situations? Any other "special chars should be taken into account?
Are you looking for Pattern.quote(String) ?
This escapes special characters for you.
EDIT:
After reading the comments, I really hope you try Pattern.quote(yourString.toLowerCase()) as it sounds like you've been using Pattern.quote(yourString).toLowerCase(). If DataNucleus is applying the regex then there should be no problems with using the \Q and \E escape sequence.
Since you have really asked for it, ".\\".replaceAll("(\\.|\\$|\\+|\\*|\\\\)", "\\\\\$1") outputs \.\\
This will escape .'s, $'s, + 's, *'s and \'s. Note that the security of this is now all upon you. If you don't escape something you needed to, or you escape it incorrectly, you will either allow people to use regex inside the search term when you weren't expecting to or it won't returns results that you were expecting.

Categories