「Java」エスケープシーケンス記号を使うサンプル

環境
JDK1.8
Eclipse 2019-12

書式

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
\\ 円記号
\' シングルクォーテーション
\" ダブルクォーテーション
\n 改行コード
\\ 円記号 \' シングルクォーテーション \" ダブルクォーテーション \n 改行コード
\\ 円記号
\' シングルクォーテーション
\" ダブルクォーテーション
\n 改行コード

タブ文字や改行文字など入力できない文字や、円記号(\)、ダブルクォーテーション(")など特別な意味を持つ文字を出力した場合はエスケープシーケンス文字を使います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.cftdemo;
public class SingDemo {
public static void main(String[] args) {
// 円記号
String strA = "\\123";
System.out.println("円記号結果: " + strA);
// ダブルクォーテーション
String strB = "test\"123";
System.out.println("ダブルクォーテーション結果: " + strB);
// 改行コード
String strC = "test\n45689";
System.out.println("改行コードの結果: " + strC);
// シングルクォーテーション
String strD = "study\'456";
System.out.println("シングルクォーテーションの結果: " + strD);
}
}
package com.arkgame.cftdemo; public class SingDemo { public static void main(String[] args) { // 円記号 String strA = "\\123"; System.out.println("円記号結果: " + strA); // ダブルクォーテーション String strB = "test\"123"; System.out.println("ダブルクォーテーション結果: " + strB); // 改行コード String strC = "test\n45689"; System.out.println("改行コードの結果: " + strC); // シングルクォーテーション String strD = "study\'456"; System.out.println("シングルクォーテーションの結果: " + strD); } }
package com.arkgame.cftdemo;

public class SingDemo {

      public static void main(String[] args) {
            // 円記号
            String strA = "\\123";
            System.out.println("円記号結果: " + strA);

            // ダブルクォーテーション
            String strB = "test\"123";
            System.out.println("ダブルクォーテーション結果: " + strB);

            // 改行コード
            String strC = "test\n45689";
            System.out.println("改行コードの結果: " + strC);

            // シングルクォーテーション
            String strD = "study\'456";
            System.out.println("シングルクォーテーションの結果: " + strD);
      }

}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
円記号結果: \123
ダブルクォーテーション結果: test"123
改行コードの結果: test
45689
シングルクォーテーションの結果: study'456
円記号結果: \123 ダブルクォーテーション結果: test"123 改行コードの結果: test 45689 シングルクォーテーションの結果: study'456
円記号結果: \123
ダブルクォーテーション結果: test"123
改行コードの結果: test
45689
シングルクォーテーションの結果: study'456

 

Java

Posted by arkgame