开发者

Is there a way to use something similar to c# 's at quoting (@" ") in java [duplicate]

开发者 https://www.devze.com 2023-01-29 21:17 出处:网络
This question already has answers here: Does Java have the '@' character to escape string quotes?
This question already has answers here: Does Java have the '@' character to escape string quotes? (3 answers) Closed 9 years ago.

In C#, if you want to read a string without having to escape the characters, you can use an at-quote

String file = @"C:\filename.txt"

which is equivalent to

String file = "C:\\开发者_运维技巧filename.txt"

Is there a simple way to escape an entire string in Java?


No, unfortunately not.

As a side note: If you don't want to support Windows 9x and ME anymore, you can use "/" as folder separator. It works on all operating systems, including Microsoft Windows since 2000.


No. And don't hardwire file paths.

What you can do is replace one character for another.

String file = reverseSlashes("C:/filename.txt");


Well in Java you would use the follow. Java like C and C++ tend to arrange things so you don't need to use \

String file = "C:/filename.txt"

EDIT: If you want to write your text without escapes first, you can cut the text as is and paste into your string and your IDE should place \, \t, and \n as required.

0

精彩评论

暂无评论...
验证码 换一张
取 消