Java中的转义序列

Java中的转义序列

转义序列是一种特殊的字符,用于指示对一组字符的不同解释方式。在Java中,转义序列是由反斜杠(\)前缀的字符。Java编译器将转义序列视为具有唯一含义的单个字符。

Java常常使用以下转义序列:

  • \t:添加一个制表符

  • :添加一个换行符

  • \r:添加一个回车符

  • \’:添加一个单引号

  • \”:添加一个双引号

  • \\:添加一个反斜杠

这些转义序列可以用于控制Java程序中文本的输出。

Java转义字符示例

  • 根据转义序列\t

  • 根据转义序列

  • 根据转义序列\b

  • 根据转义序列\r

  • 根据转义序列\f

  • 根据转义序列\\’

  • 根据转义序列\\”

示例1:根据转义序列\t

此代码使用System.out.println语句显示一条消息。该消息包含由制表符(\t)分隔的”Hello Everyone”和”Welcome”。当打印时,它在单词之间创建了一个间隔。

算法

  • 步骤1:创建一个名为TLP的类。

  • 步骤2:在TLP类中创建一个名为main()的方法。

  • 步骤3:在main()方法中,声明一个名为message的字符串变量,并将其设置为”Hello Everyone\tWelcome “的值。

  • 步骤4:使用System.out.println()方法打印message变量的值。

  • 步骤5:message变量中的\t转义序列将在”Hello Everyone”和”Welcome”之间插入一个制表符。

  • 步骤6:System.out.println()方法将打印输出。

示例

public class TLP {
public static void main(String[] args) 
{ 
// \t: It offers a tab between two words.
  System.out.println("Hello Everyone\tWelcome ");
  }
}

输出

Hello Everyone  Welcome

示例2:根据转义序列

提供的代码使用System.out.println语句来显示一条消息。该消息包括“Hello Everyone!”和“Welcome to tutorialspoint”,之间插入了一个
转义序列。当打印时,这个序列可以使消息的第二部分显示在新的一行上。

算法

  • 步骤1:在main()方法中,声明一个名为message的字符串变量,并将其设置为值。在这个例子中,我们取的是“Hello Everyone!
    Welcome to tutorialspoint”。

  • 步骤2:使用System.out.println()方法打印消息变量的值。

  • 步骤3:消息变量中的转义序列帮助在“Hello Everyone!”一词后插入一个新行。

  • 步骤4:打印输出结果。

示例

//   
 escape sequence is employed for a new line.

public class TLP {
    public static void main(String[] args)
    {
        System.out.println("Hello Everyone!   
 Welcome to tutorialspoint  ");
    }
}

输出

Hello Everyone! 
 Welcome to tutorialspoint

示例3:根据转义序列\b

提供的代码使用System.out.println语句来显示一条消息。该消息包括“Hello World”后面跟着\b转义序列和“Good Evening!”。\b转义序列作为一个退格字符,在某些情况下可以擦除它之前的字符或将光标向后移动一步。

算法

  • 步骤1:定义一个名为TLP的公共类。

  • 步骤2:在主方法的大括号内,使用System.out.println()将文本输出到控制台。

  • 步骤3:在System.out.println()括号中的文本内容中,包含短语“Hello World\b Good Evening!”

  • 步骤4:\b代表退格转义序列。

示例

// The escape sequence \b is a backspace character
//Depending on the compiler, it either deletes the character or shifts the cursor back one character.
public class TLP {
    public static void main(String[] args)
    {
        System.out.println("Hello World\b Good Evening! ");
    }
}

输出

Hello World Good Evening!

示例4:根据转义序列\r

给定的代码使用System.out.println语句显示一条消息。该消息包含”Welcome Students”后跟\r转义序列和”to tutorialspoint”。

算法

  • 步骤1:在main()方法内部,声明一个名为message的字符串变量,并将其设置为”Welcome Students \r to tutorialspoint “的值。

  • 步骤2:使用System.out.println()方法打印message变量的值。

  • 步骤3:message变量中的\r转义序列将把光标移动到行首,而不会打印新的一行。

  • 步骤4:打印输出。

示例

public class TLP {
public static void main(String[] args) 
{ 
// \r: Without generally progressing along a line, it returns the output point to the line's beginning.

  System.out.println("Welcome Students \r to tutorialspoint ");
  }
}

输出

Welcome Students 
 to tutorialspoint

例子5:根据转义序列\f进行展示

在下面的代码中,消息包括”大家好!”,接着是转义序列\f和”你们都好吗?”。转义序列\f用于表示换页,但在某些环境中可能看不到效果。

算法

  • 步骤1:在main()方法中,声明一个名为message的字符串变量,并将其设置为”大家好!\f你们都好吗?”

  • 步骤2:使用System.out.println()方法打印message变量的值。

  • 步骤3:消息变量中的\f转义序列将插入一个换页符,将导致输出移动到下一页的顶部。

  • 步骤4:打印输出。

例子

// \f escape sequence is a form feed character used to indicate a page break.

public class TLP {
    public static void main(String[] args)
    {
        System.out.println("Hello Everyone! \f Are you all doing good? ");
    }
}

输出

Hello Everyone!  Are you all doing good?

示例6:根据转义序列\’情况而定

此代码是使用System.out.println语句显示一条消息的Java程序。该消息包含”Hello this is ‘Tutorialspoint'”,其中使用了’转义序列来在文本中打印单引号。

算法

  • 步骤1:在main()方法中,声明一个名为message的字符串变量。然后将其设置为”Hello this is ‘Tutorialspoint'”的值。

  • 步骤2:使用System.out.println()方法打印message变量的值。

  • 步骤3:在message变量中的’转义序列告诉编译器单引号是字符串的一部分,而不是字符串的结尾。

  • 步骤4:打印输出。

示例

public class TLP {
public static void main(String[] args) 
{ 
// '' is employed to print a single quotation mark on the text string.
  System.out.println("Hello this is 'Tutorialspoint'");
  }
}

输出

Hello this is 'Tutorialspoint'

例子7:根据转义序列\”

算法

  • 步骤1:在main()方法中,声明一个名为messag的字符串变量。将其设置为值”Hello World “This is Tutorialspoint””。

  • 步骤2:使用System.out.println()方法打印消息变量的值。

  • 步骤3:消息变量中的”转义序列将告诉编译器双引号是字符串的一部分,而不是字符串的结尾。

  • 步骤4:打印输出。

例子

public class TLP {
public static void main(String[] args) 
{ 
// " is used to print a double quotation mark on the text string.
  System.out.println("Hello World "This is Tutorialspoint"");
  }
}

输出

Hello World "This is Tutorialspoint"

结论

转义序列是一种特殊类型的字符,在Java中用于表示一系列字符的另一种解释方式。它们通常用于控制Java程序中的文本输出。

Java中常用的转义序列包括\t、\n 、\r、’、”和\\。转义序列可以用于在文本中插入制表符、换行符、回车符、单引号、双引号和反斜杠等操作。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程