1. 程式人生 > >幾個特殊字元在String中的轉義表示式

幾個特殊字元在String中的轉義表示式

1、未解決的問題:有點奇怪的是:回退字元,並沒有出現大家說的問題,就是出現回退字元的後面字元會吃掉前面的一個字元。不知道是為什麼。

2、測試code:

public class Main{
    public static void main(String[] args) {
        // Scanner in = new Scanner(System.in);
        //單引號
        String str1="I\'love\'working!";
        //雙引號
        String str2="I\"love\"working!";
        //反斜線
        String str3="I\\love\\working!";
        //製表符
        String str4="I\tlove\tworking!";
        //回退符
        String str5="I \blove \bworking!";
        //回車符:將當前位置移到本行開始:故會覆蓋\r之前輸出的字元
        String str6="I\rlove\rworking!";
        //走紙符
        String str7="I\flove\fworking!";
        //換行符:將當前位置移到下一行開始
        String str8="I\nlove\nworking!";

        //輸出:
        System.out.println("單引號--------------");
        System.out.println(str1);
        System.out.println();

        System.out.println("雙引號--------------");
        System.out.println(str2);
        System.out.println();

        System.out.println("反斜線--------------");
        System.out.println(str3);
        System.out.println();

        System.out.println("製表符--------------");
        System.out.println(str4);
        System.out.println();

        System.out.println("回退符--------------");
        System.out.println(str5);
        System.out.println();

        System.out.println("回車符--------------");
        System.out.println(str6);
        System.out.println();

        System.out.println("走紙符--------------");
        System.out.println(str7);
        System.out.println();

        System.out.println("換行符--------------");
        System.out.println(str8);
        System.out.println();

    }
}

輸出結果:

單引號--------------
I'love'working!

雙引號--------------
I"love"working!

反斜線--------------
I\love\working!

製表符--------------
I	love	working!

回退符--------------
Iloveworking!

回車符--------------
working!

走紙符--------------
I love working!

換行符--------------
I
love
working!