将双反斜杠作为普通文本符号包含在字符串中

huangapple go评论79阅读模式
英文:

Include Double Backslash as plain text symbols in a String

问题

我有一个普通的字符串,其中包含"\"。然而,Java 似乎在看到双反斜杠时尝试对字符串进行格式化。有没有办法让字符串完全忽略反斜杠的格式化?

String Test = "文件位置在:C:\\Folder\\File.txt";

输出:

Test = "文件位置在:C:\Folder\File.txt";

期望输出:

Test = "文件位置在:C:\\Folder\\File.txt";
英文:

I have a plain String where it contains "\". However Java seems to be trying to format the String when it sees the Double Backslash. Is there a way to tell the String to completely ignore Backslash formatting?

String Test = "File location is: C:\\Folder\\File.txt";

Output:

Test = "File location is: C:\Folder\File.txt"

Desired output:

Test = "File location is: C:\\Folder\\File.txt"

答案1

得分: 0

尝试这个

    public class Sample {
    
         public static void main(String[] args){
            String test = "文件位置是:C:\\\\文件夹\\\\文件.txt";
            System.out.println("测试 = "+ test);
         }
    }

输出

    测试 = 文件位置是C:\\文件夹\\文件.txt
英文:

Try this :

public class Sample {

     public static void main(String[] args){
        String test = "File location is: C:\\\\Folder\\\\File.txt";
        System.out.println("Test = "+ test);
     }
}

Output :

Test = File location is: C:\\Folder\\File.txt

huangapple
  • 本文由 发表于 2020年9月17日 21:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63939261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定