Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * author: Nick Humphrey, http://nickhumphreyit.blogspot.no/
- * date: 20131202
- *
- * description: testing escape sequences
- *
- * characters, escape sequences:
- * http://docs.oracle.com/javase/tutorial/java/data/characters.html
- *
- * java.util.regex.Pattern:
- * http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
- * */
- public class EscapeSequencesTest {
- public static void main(String[] args) {
- /*
- * If the number of consecutive backslashes is even (e.g. \\\\...), then the \ is eligible to begin a Unicode
- * escape;
- * if odd, then the \ is not eligible to begin a Unicode escape (e.g. \\\...)
- *
- * OBSERVATION:
- * if number of \ is even, output: (number of \) / 2
- * if number of \ is odd, output: (number of \) % 2 + 1
- */
- System.out.println("\\u2126=\u2126"); // OUT: \u2126=Ω
- System.out.println("\\\\u2126=\\\u2126"); // OUT: \\u2126=\Ω
- System.out.println("\\\\\\u2126=\\\\\u2126"); // OUT: \\\u2126=\\Ω
- /*
- * If an eligible \ is not followed by u, then it is treated as a RawInputCharacter and
- * remains part of the escaped Unicode stream.
- */
- System.out.println("\\\f123"); // OUT: \123 (the unprintable character is "box 000C")
- System.out.println("\\123"); // OUT: \123
- /*
- * If an eligible \ is followed by u, or more than one u, and the last u is not followed
- * by four hexadecimal digits, then a compile-time error occurs.
- *
- * OUT: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Invalid unicode
- */
- // System.out.println("\ uu123"); // (but without a space between "\" and "u"--otherwise eclipse gives an error)
- /*
- * For example, the raw input \u005cu005a results in the six characters \ u 0 0 5 a,
- * because 005c is the Unicode value for \
- *
- * OUT:
- * Exception in thread "main" java.lang.Error: Unresolved compilation problems:
- * Invalid unicode
- * Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
- */
- // System.out.println("\u005cu005a");
- /*
- * regexp:
- * http://docs.oracle.com/javase/1.5.0/docs/api/java/util/regex/Pattern.html
- *
- * (EXAMPLES IN THE LIST BELOW SHOULD BE WITHOUT A SPACE BETWEEN \ AND FIRST CONTIGUOUS CHAR.
- * SPACES ARE INSERTED SO THE FILE WOULD COMPILE)
- * The character with octal value 0n (0 <= n <= 7)
- * \0n
- * The character with octal value 0nn (0 <= n <= 7)
- * \0nn
- * The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
- * \0mnn
- * The character with hexadecimal value 0xhh
- * \ xhh
- * The character with hexadecimal value 0xhhhh
- * \ uhhhh
- * The tab character ('\u0009')
- * \t
- * The newline (line feed) character (' ')
- * \n
- * The carriage-return character (' ')
- * \r
- * The form-feed character ('\u000C')
- * \f
- * The alert (bell) character ('\u0007')
- * \a
- * The escape character ('\u001B')
- * \e
- * The control character corresponding to x
- * \ cx
- */
- // The character with octal value 0n (0 <= n <= 7)
- System.out.println("\00"); // OUT: (blank/nothing)
- System.out.println("\01"); // OUT:
- System.out.println("\08"); // OUT: (blank/nothing)
- // The character with octal value 0nn (0 <= n <= 7)
- System.out.println("\0nn"); // OUT: (blank/nothing)
- System.out.println("\007"); // OUT:
- // The character with octal value 0mnn (0 <= m <= 3, 0 <= n <= 7)
- System.out.println("\0mnn"); // OUT: (blank/nothing)
- System.out.println("\0207"); // OUT: 7
- // The character with hexadecimal value 0xhh
- // System.out.println("\xhh");
- // System.out.println("\x0f");
- System.out.println("\0x3d"); // OUT: (blank/nothing)
- // The character with hexadecimal value 0xhhhh
- // System.out.println("\ uhhhh");
- System.out.println("\u0fa4"); // OUT: -ྤ
- System.out.println("\0u0fa4"); // OUT: (blank/nothing)
- System.out.println("\0x0fa4"); // OUT: (blank/nothing)
- // The tab character ('\u0009')
- System.out.println("\t"); // OUT: (tab)
- // The newline (line feed) character,
- System.out.println("\n"); // OUT: (new line)
- // The carriage-return character,
- System.out.println("\r"); // OUT: (blank/nothing)
- // The form-feed character ('\u000C')
- // 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)
- System.out.println("\f"); // OUT:
- // The alert (bell) character ('\u0007')
- /*
- * COMPILE ERROR: Got an exception - unexpected char: 'a'
- * CAUSE: checkstyle
- * FIX: right click in file > checkstyle > clear checkstyle violations
- *
- * COMPILE ERROR: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
- */
- // System.out.println("\a");
- // The escape character ('\u001B')
- // COMPILE ERROR: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
- // System.out.println("\e");
- // The control character corresponding to x
- // COMPILE ERROR: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
- // System.out.println("\cX");
- // System.out.println("\c?");
- // System.out.println("\c0");
- // System.out.println("\cf");
- // System.out.println("\cB");
- /*
- * characters, escape sequences:
- * http://docs.oracle.com/javase/tutorial/java/data/characters.html
- */
- // \b Insert a backspace in the text at this point.
- System.out.println("x\b"); // OUT: x
- System.out.println("\1"); // OUT:
- char character = 'a';
- int ascii = (int) character;
- System.out.println(ascii); // OUT: 97
- System.out.println("\097"); // OUT:
- System.out.println("\123"); // OUT: S
- System.out.println("\124"); // OUT: T
- System.out.println("\1234567890"); // OUT: S4567890
- /*
- * OUT:
- * (empty/new line)
- * 3
- */
- System.out.println("\0123");
- /*
- * OUT:
- * Exception in thread "main" java.lang.Error: Unresolved compilation problem:
- * Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
- */
- // System.out.println("\ 256"); // compile error when uncommented
- /*
- * OUT:
- * (empty/new line)
- * É
- */
- System.out.println("\12\311");
- System.out.println("--"); // control output, to denote last line
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment