Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore escape sequences in strings java?

Tags:

java

c#

In C#, I can write the string "\\myDir\\myFile" as @"\myDir\myFile". What is the equivalent for this "@" character in Java, if any?

like image 495
Hari Menon Avatar asked Sep 24 '10 08:09

Hari Menon


People also ask

How do I ignore an escape character in a string?

To ignoring escape sequences in the string, we make the string as "raw string" by placing "r" before the string. "raw string" prints as it assigned to the string.

How do you avoid escape sequences?

For turning a normal string into a raw string, prefix the string (before the quote) with an r or R. This is the method of choice for overcoming this escape sequence problem.

How do you ignore a string in Java?

Java String equalsIgnoreCase() Method The equalsIgnoreCase() method compares two strings, ignoring lower case and upper case differences. This method returns true if the strings are equal, and false if not. Tip: Use the compareToIgnoreCase() method to compare two strings lexicographically, ignoring case differences.

How do you skip a char in Java?

In Java, if a character is preceded by a backslash (\) is known as Java escape sequence or escape characters. It may include letters, numerals, punctuations, etc. Remember that escape characters must be enclosed in quotation marks (""). These are the valid character literals.


2 Answers

To my knowledge no such equivalence exist in the Java language definition.

like image 152
rsp Avatar answered Oct 23 '22 10:10

rsp


The easiest way is to Use Unix-style paths in Java. Java will find out what the real paths are in all File-based code.

System.out.println(new File("c:/dev/m2-repo/org/apache/ant").getCanonicalPath());

Output:

C:\dev\m2-repo\org\apache\ant

BTW if it's the root drive, you can skip the drive letter. Java will understand /programs if you look for C:\programs

like image 22
Sean Patrick Floyd Avatar answered Oct 23 '22 08:10

Sean Patrick Floyd