使用字符字面量转义序列的Java程序
转义序列是一个字符或一个字符序列,其目的是实现它们不能按字面表示的字符,否则可能无法表示。此类字符的一些示例包括退格、换行、水平或垂直制表符等。在Java中,这些序列由前面的反斜杠 '\'表示,属于字符数据类型。 Java中一共有8个转义序列。它们列在下面:
Tab : '\t'
Newline : '\n'
Backspace : '\b'
Form feed : '\f'
Carriage return : '\r'
Backslash : '\\'
Single quote : '\''
Double quote : '\"'
1.Type Casting: Type Casting是转换的概念 一种原始数据类型转换为另一种.有两种类型的类型转换:
- 扩展或自动类型转换:在此过程中,较低优先级的原始数据类型会自动转换为另一种较高优先级的原始数据类型。例如,从int转换为double 。
- 缩小或显式类型转换:这是将优先级较高的原始数据类型转换为优先级较低的数据类型的过程。例如,从int到char的转换。
我们将在这里使用显式类型转换。更具体地说,从int到char的类型转换。
2. ASCII 码: ASCII 码用于用数字来表示字符。它是一个统一的系统,允许计算机解释所有字符。 ASCII 代表美国信息交换标准代码。例如,该字符的 ASCII 码为97 。
有两种方法可以在Java中将转义序列存储在字符字面量中。它们列出如下:
- 在单引号内使用普通转义序列
- 通过使用显式类型转换将转义序列的 ASCII 码转换为字符字面量
方法一:
在这种方法中,我们使用最简单的方法,即直接将转义序列存储为字符字面量。
代码 :
Java
// Java code to store escape sequences
// as character literals
class StoreCharacterLiterals {
// the method to print the escape
// sequences
static void escapeSequences()
{
// declaration of Character
// literals
char tab = '\t', backspace = '\b', newline = '\n',
formFeed = '\f', carriageReturn = '\r',
singleQuote = '\'', doubleQuote = '\"',
backSlash = '\\';
// printing the horizontal tab
System.out.println("This" + tab + " is an "
+ "implementation of tab");
// implementing the backspace
System.out.println("This" + backspace + " is an "
+ "implementation "
+ "of backspace");
// implementing the newline
System.out.println("This" + newline + " is an "
+ "implementation of newline");
// implementing the formfeed
System.out.println("This" + formFeed + " is an"
+ " implementation of "
+ "formfeed");
// implementing the carriage return
System.out.println("This" + carriageReturn
+ " is an implementation "
+ "of Carriage Return");
// printing the single quote
System.out.println("This" + singleQuote
+ " is an implementation"
+ " of single quote");
// printing the double quote
System.out.println("This" + doubleQuote
+ " is an implementation "
+ "of double quote");
// printing the backslash
System.out.println("This" + backSlash
+ " is an implementation "
+ " of backslash");
}
// Driver Code
public static void main(String[] args)
{
// creating an object of the class
StoreCharacterLiterals ob = new StoreCharacterLiterals();
// calling the escapeSequences() method
ob.escapeSequences();
}
}
Java
// Java code to store escape sequences
// as character literals
class StoreCharacterLiterals {
// the method to print the escape
// sequences
static void escapeSequences()
{
// declaration of Character
// literals using their ASCII codes
// ASCII code for tab : 9
// ASCII code for backspace : 8
// ASCII code for newline : 10
// ASCII code for formfeed : 12
// ASCII code for carriage return : 13
// ASCII code for single quote : 39
// ASCII code for double quote : 34
// ASCII code for backslash : 92
char tab = 9, backspace = 8, newline = 10,
formFeed = 12, carriageReturn = 13,
singleQuote = 39, doubleQuote = 34,
backSlash = 92;
// printing the horizontal tab
System.out.println("This" + tab + " is an "
+ "implementation of tab");
// implementing the backspace
System.out.println("This" + backspace + " is an "
+ "implementation "
+ "of backspace");
// implementing the newline
System.out.println("This" + newline + " is an "
+ "implementation of newline");
// implementing the formfeed
System.out.println("This" + formFeed + " is an"
+ " implementation of "
+ "formfeed");
// implementing the carriage return
System.out.println("This" + carriageReturn
+ " is an implementation "
+ "of Carriage Return");
// printing the single quote
System.out.println("This" + singleQuote
+ " is an implementation"
+ " of single quote");
// printing the double quote
System.out.println("This" + doubleQuote
+ " is an implementation "
+ "of double quote");
// printing the backslash
System.out.println("This" + backSlash
+ " is an implementation "
+ " of backslash");
}
// Driver Code
public static void main(String[] args)
{
// creating an object of the class
StoreCharacterLiterals ob = new StoreCharacterLiterals();
// calling the escapeSequences() method
ob.escapeSequences();
}
}
输出
This is an implementation of tab
Thi is an implementation of backspace
This
is an implementation of newline
This
is an implementation of formfeed
is an implementation of Carriage Return
This' is an implementation of single quote
This" is an implementation of double quote
This\ is an implementation of backslash
方法二:
按照这种方法,我们使用从int到char的显式类型转换将转义序列存储在字符字面量中。
Java
// Java code to store escape sequences
// as character literals
class StoreCharacterLiterals {
// the method to print the escape
// sequences
static void escapeSequences()
{
// declaration of Character
// literals using their ASCII codes
// ASCII code for tab : 9
// ASCII code for backspace : 8
// ASCII code for newline : 10
// ASCII code for formfeed : 12
// ASCII code for carriage return : 13
// ASCII code for single quote : 39
// ASCII code for double quote : 34
// ASCII code for backslash : 92
char tab = 9, backspace = 8, newline = 10,
formFeed = 12, carriageReturn = 13,
singleQuote = 39, doubleQuote = 34,
backSlash = 92;
// printing the horizontal tab
System.out.println("This" + tab + " is an "
+ "implementation of tab");
// implementing the backspace
System.out.println("This" + backspace + " is an "
+ "implementation "
+ "of backspace");
// implementing the newline
System.out.println("This" + newline + " is an "
+ "implementation of newline");
// implementing the formfeed
System.out.println("This" + formFeed + " is an"
+ " implementation of "
+ "formfeed");
// implementing the carriage return
System.out.println("This" + carriageReturn
+ " is an implementation "
+ "of Carriage Return");
// printing the single quote
System.out.println("This" + singleQuote
+ " is an implementation"
+ " of single quote");
// printing the double quote
System.out.println("This" + doubleQuote
+ " is an implementation "
+ "of double quote");
// printing the backslash
System.out.println("This" + backSlash
+ " is an implementation "
+ " of backslash");
}
// Driver Code
public static void main(String[] args)
{
// creating an object of the class
StoreCharacterLiterals ob = new StoreCharacterLiterals();
// calling the escapeSequences() method
ob.escapeSequences();
}
}
输出
This is an implementation of tab
Thi is an implementation of backspace
This
is an implementation of newline
This
is an implementation of formfeed
is an implementation of Carriage Return
This' is an implementation of single quote
This" is an implementation of double quote
This\ is an implementation of backslash
注意:在控制台(Windows)或终端(Ubuntu)中运行代码。代码在控制台上输出为\b 。有关更多信息,请阅读: https://stackoverflow.com/questions/3328824/java-backspace-escape