专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java特殊字符的使用

Java特殊字符的使用

更新时间:2022-09-23 10:30:28 来源:动力节点 浏览790次

因为字符串必须写在引号内,Java 会误解这个字符串,并产生错误:

String txt = "We are the so-called "Vikings" from the north.";

避免这个问题的解决方案是使用反斜杠转义字符。

反斜杠 ( \) 转义字符将特殊字符转换为字符串字符:

Escape character Result Description
\' ' Single quote
\" " Double quote
\\ \ Backslash

该序列\" 在字符串中插入双引号:

String txt = "We are the so-called \"Vikings\" from the north.";

该序列\' 在字符串中插入一个单引号:

String txt = "It\'s alright.";

该序列\\ 在字符串中插入一个反斜杠:

String txt = "The character \\ is called backslash.";

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>