print_escaped

print_escaped(io, str::AbstractString, esc::AbstractString)

General escaping of traditional C and Unicode escape sequences, plus any characters in esc are also escaped (with a backslash).

Examples

In the Julia programming language, the function print_escaped(io, str::AbstractString, esc::AbstractString) is used for general escaping of traditional C and Unicode escape sequences, with the additional capability of escaping any characters specified in esc with a backslash. This function is typically used to print strings with special characters escaped for display or storage.

Here are some examples of how the print_escaped function can be used:

  1. Escape special characters in a string:

    julia> io = IOBuffer();
    julia> print_escaped(io, "Hello\tWorld!\n", "\t\n")
    julia> String(take!(io))
    "Hello\\tWorld!\\n"

    This example prints the string "Hello\tWorld!\n" with the tab and newline characters escaped. The output is "Hello\tWorld!\n".

  2. Escape Unicode characters:

    julia> io = IOBuffer();
    julia> print_escaped(io, "αβγδ", "")
    julia> String(take!(io))
    "\\u{3b1}\\u{3b2}\\u{3b3}\\u{3b4}"

    It escapes Unicode characters in the string "αβγδ" using the \u{} notation. The output is "\u{3b1}\u{3b2}\u{3b3}\u{3b4}".

  3. Escape specific characters using custom escape sequence:
    julia> io = IOBuffer();
    julia> print_escaped(io, "Hello, World!", "o")
    julia> String(take!(io))
    "Hell\\o, W\\o\\rld!"

    In this example, the characters 'o' in the string "Hello, World!" are escaped with a backslash. The output is "Hell\o, W\o\rld!".

Common mistake example:

julia> io = IOBuffer();
julia> print_escaped(io, "Hello, World!", "\\")
ERROR: syntax: invalid escape sequence

In this example, the backslash character "\" is used as the esc argument. However, it results in an error because "\" is an invalid escape sequence in Julia. Make sure to use valid escape sequences or characters when specifying the esc argument.

See Also

:@printf, :@sprintf, display, displayable, dump, info, isprint, print, println, print_escaped, print_joined, print_shortest, print_unescaped, print_with_color, pushdisplay, redisplay, show, showall, showcompact, sprint, versioninfo,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: