nickhumphrey

Escape Sequences Test

Dec 3rd, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.20 KB | None | 0 0
  1. /*
  2.  * author: Nick Humphrey, http://nickhumphreyit.blogspot.no/
  3.  * date: 20131202
  4.  *
  5.  * description: testing escape sequences
  6.  *
  7.  * characters, escape sequences:
  8.  * http://docs.oracle.com/javase/tutorial/java/data/characters.html
  9.  *
  10.  * java.util.regex.Pattern:
  11.  * http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
  12.  * */
  13. public class EscapeSequencesTest {
  14.     public static void main(String[] args) {
  15.         /*
  16.          * If the number of consecutive backslashes is even (e.g. \\\\...), then the \ is eligible to begin a Unicode
  17.          * escape;
  18.          * if odd, then the \ is not eligible to begin a Unicode escape (e.g. \\\...)
  19.          *
  20.          * OBSERVATION:
  21.          * if number of \ is even, output: (number of \) / 2
  22.          * if number of \ is odd, output: (number of \) % 2 + 1
  23.          */
  24.         System.out.println("\\u2126=\u2126"); // OUT: \u2126=Ω
  25.         System.out.println("\\\\u2126=\\\u2126"); // OUT: \\u2126=\Ω
  26.         System.out.println("\\\\\\u2126=\\\\\u2126"); // OUT: \\\u2126=\\Ω
  27.  
  28.         /*
  29.          * If an eligible \ is not followed by u, then it is treated as a RawInputCharacter and
  30.          * remains part of the escaped Unicode stream.
  31.          */
  32.         System.out.println("\\\f123"); // OUT: \ 123 (the unprintable character is "box 000C")
  33.         System.out.println("\\123"); // OUT: \123
  34.  
  35.         /*
  36.          * If an eligible \ is followed by u, or more than one u, and the last u is not followed
  37.          * by four hexadecimal digits, then a compile-time error occurs.
  38.          *
  39.          * OUT: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Invalid unicode
  40.          */
  41. //      System.out.println("\ uu123"); // (but without a space between "\" and "u"--otherwise eclipse gives an error)
  42.  
  43.         /*
  44.          * For example, the raw input \u005cu005a results in the six characters \ u 0 0 5 a,
  45.          * because 005c is the Unicode value for \
  46.          *
  47.          * OUT:
  48.          * Exception in thread "main" java.lang.Error: Unresolved compilation problems:
  49.          * Invalid unicode
  50.          * Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
  51.          */
  52. //      System.out.println("\u005cu005a");
  53.  
  54.         /*
  55.          * regexp:
  56.          * http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
  57.          *
  58.          * (EXAMPLES IN THE LIST BELOW SHOULD BE WITHOUT A SPACE BETWEEN \ AND FIRST CONTIGUOUS CHAR.
  59.          * SPACES ARE INSERTED SO THE FILE WOULD COMPILE)
  60.          * The character with octal value 0n (0 <= n <= 7)
  61.          * \0n
  62.          * The character with octal value 0nn (0 <= n <= 7)
  63.          * \0nn
  64.          * The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
  65.          * \0mnn
  66.          * The character with hexadecimal value 0xhh
  67.          * \ xhh
  68.          * The character with hexadecimal value 0xhhhh
  69.          * \ uhhhh
  70.          * The tab character ('\u0009')
  71.          * \t
  72.          * The newline (line feed) character (' ')
  73.          * \n
  74.          * The carriage-return character (' ')
  75.          * \r
  76.          * The form-feed character ('\u000C')
  77.          * \f
  78.          * The alert (bell) character ('\u0007')
  79.          * \a
  80.          * The escape character ('\u001B')
  81.          * \e
  82.          * The control character corresponding to x
  83.          * \ cx
  84.          */
  85.  
  86.         // The character with octal value 0n (0 <= n <= 7)
  87.         System.out.println("\00"); // OUT: (blank/nothing)
  88.         System.out.println("\01"); // OUT: 
  89.         System.out.println("\08"); // OUT: (blank/nothing)
  90.  
  91.         // The character with octal value 0nn (0 <= n <= 7)
  92.         System.out.println("\0nn"); // OUT: (blank/nothing)
  93.         System.out.println("\007"); // OUT: 
  94.  
  95.         // The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
  96.         System.out.println("\0mnn"); // OUT: (blank/nothing)
  97.         System.out.println("\0207"); // OUT: 7
  98.  
  99.         // The character with hexadecimal value 0xhh
  100. //      System.out.println("\xhh");
  101. //      System.out.println("\x0f");
  102.         System.out.println("\0x3d"); // OUT: (blank/nothing)
  103.  
  104.         // The character with hexadecimal value 0xhhhh
  105. //      System.out.println("\ uhhhh");
  106.         System.out.println("\u0fa4"); // OUT: -ྤ
  107.         System.out.println("\0u0fa4"); // OUT: (blank/nothing)
  108.         System.out.println("\0x0fa4"); // OUT: (blank/nothing)
  109.  
  110.         // The tab character ('\u0009')
  111.         System.out.println("\t"); // OUT: (tab)
  112.  
  113.         // The newline (line feed) character,
  114.  
  115.         System.out.println("\n"); // OUT: (new line)
  116.  
  117.         // The carriage-return character,
  118.         System.out.println("\r"); // OUT: (blank/nothing)
  119.  
  120.         // The form-feed character ('\u000C')
  121.         // NOTE: ('\u000C') in the comment doesn't give compile errors like examples above, e.g., '\ u000D' (but without a space between "\" and "u"--otherwise eclipse gives an error)
  122.         System.out.println("\f"); // OUT:
  123.  
  124.         // The alert (bell) character ('\u0007')
  125.         /*
  126.          * COMPILE ERROR: Got an exception - unexpected char: 'a'
  127.          * CAUSE: checkstyle
  128.          * FIX: right click in file > checkstyle > clear checkstyle violations
  129.          *
  130.          * COMPILE ERROR: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
  131.          */
  132. //      System.out.println("\a");
  133.  
  134.         // The escape character ('\u001B')
  135.         // COMPILE ERROR: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
  136. //      System.out.println("\e");
  137.  
  138.         // The control character corresponding to x
  139.         // COMPILE ERROR: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
  140. //      System.out.println("\cX");
  141. //      System.out.println("\c?");
  142. //      System.out.println("\c0");
  143. //      System.out.println("\cf");
  144. //      System.out.println("\cB");
  145.  
  146.         /*
  147.          * characters, escape sequences:
  148.          * http://docs.oracle.com/javase/tutorial/java/data/characters.html
  149.          */
  150.         // \b  Insert a backspace in the text at this point.
  151.         System.out.println("x\b"); // OUT: x
  152.  
  153.         System.out.println("\1"); // OUT: 
  154.  
  155.         char character = 'a';    
  156.         int ascii = (int) character;
  157.         System.out.println(ascii); // OUT: 97
  158.         System.out.println("\097"); // OUT:
  159.         System.out.println("\123"); // OUT: S
  160.         System.out.println("\124"); // OUT: T
  161.         System.out.println("\1234567890"); // OUT: S4567890
  162.  
  163.         /*
  164.          * OUT:
  165.          * (empty/new line)
  166.          * 3
  167.          */
  168.         System.out.println("\0123");
  169.  
  170.         /*
  171.          * OUT:
  172.          * Exception in thread "main" java.lang.Error: Unresolved compilation problem:
  173.          * Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
  174.          */
  175. //      System.out.println("\ 256");    // compile error when uncommented
  176.  
  177.         /*
  178.          * OUT:
  179.          * (empty/new line)
  180.          * É
  181.          */
  182.         System.out.println("\12\311");
  183.  
  184.         System.out.println("--"); // control output, to denote last line
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment