且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java如何在字符串数组中定义特殊字符

更新时间:2023-02-26 11:42:54

\" 是 String 类中的特殊字符

\ and " are special characters in String class

  • " 是字符串的开始或结束
  • \ 用于创建一些字符,如新行 \n \r tab\t 或转义特殊字符,例如您的情况 \"
  • " is start or end of String
  • \ is used to create some characters like new lines \n \r tab\t or to escape special characters like in your case \ and "

所以要使它们成为文字,您必须使用 "\\""\""

So to make them literals you will have to escape them with "\\" and "\""

其他想法是使用 Character[] 而不是 String[] 这样你就不必转义 " 并且你的字符可以被写入作为 '"''\\' (因为 ' 需要转义 - 它应该写成 '\''code> - \ 在这里也很特殊,也需要转义以生成其文字).

Other idea is to use Character[] instead of String[] so you wont have to escape " and yours characters can be written as '"' or '\\' (because ' require escaping - it should be written as '\'' - \ is also special here and will also require escaping to produce its literal).