# Julia offers excellent support for string formatting using
# string interpolation and the `println` function. Here are some
# examples of common string formatting tasks.
struct Point
x :: Int
y :: Int
end
# Julia offers several string interpolation methods designed to
# format general Julia values. For example, this prints
# an instance of our `Point` struct.
p = Point ( 1 , 2 )
println ( "struct1: $p " )
# If the value is a struct, you can access its fields directly
# in the string interpolation.
println ( "struct2: (x= $ ( p . x ) , y= $ ( p . y ) )" )
# To print a more detailed representation of the value,
# you can use the `repr` function.
println ( "struct3: $ ( repr ( p )) " )
# To print the type of a value, use `typeof`.
println ( "type: $ ( typeof ( p )) " )
# Formatting booleans is straightforward.
println ( "bool: $ ( true ) " )
# There are many options for formatting integers.
# Use standard string interpolation for base-10 formatting.
println ( "int: $ ( 123 ) " )
# This prints a binary representation.
println ( "bin: $ ( bitstring ( 14 )) " )
# This prints the character corresponding to the given integer.
println ( "char: $ ( Char ( 33 )) " )
# `string(x, base=16)` provides hex encoding.
println ( "hex: $ ( string ( 456 , base = 16 )) " )
# There are also several formatting options for floats.
# For basic decimal formatting, use standard string interpolation.
println ( "float1: $ ( 78.9 ) " )
# `@sprintf` macro from the Printf package can be used for more
# control over float formatting.
using Printf
@printf ( "float2: %.2e \n " , 123400000.0 )
@printf ( "float3: %.2E \n " , 123400000.0 )
# For basic string printing, use standard string interpolation.
println ( "str1: $ ( " \" string \" " ) " )
# To include quotes in strings, you can use escape characters.
println ( "str2: \"\\\" string \\\"\" " )
# To print a hexadecimal representation of a string,
# you can use the `bytes2hex` function.
println ( "str3: $ ( bytes2hex ( Vector { UInt8 }( "hex this" ))) " )
# To print a representation of a pointer, use the `pointer` function.
println ( "pointer: $ ( pointer_from_objref ( p )) " )
# When formatting numbers you will often want to control the width
# and precision of the resulting figure. The `@sprintf` macro
# can be used for this purpose.
@printf ( "width1: | %6d | %6d | \n " , 12 , 345 )
# You can also specify the width of printed floats,
# though usually you'll also want to restrict the decimal precision
# at the same time with the width.precision syntax.
@printf ( "width2: | %6.2f | %6.2f | \n " , 1.2 , 3.45 )
# To left-justify, use the `-` flag.
@printf ( "width3: | %-6.2f | %-6.2f | \n " , 1.2 , 3.45 )
# You may also want to control width when formatting strings,
# especially to ensure that they align in table-like output.
# For basic right-justified width:
@printf ( "width4: | %6s | %6s | \n " , "foo" , "b" )
# To left-justify use the `-` flag as with numbers.
@printf ( "width5: | %-6s | %-6s | \n " , "foo" , "b" )
# So far we've seen `println`, which prints the formatted string
# to stdout. To format and return a string without printing it,
# you can use string interpolation or the `@sprintf` macro.
s = "sprintf: a $ ( "string" ) "
println ( s )
# You can format+print to IO streams other than stdout
# using the `print` function with an IO object.
print ( stderr , "io: an " , "error \n " )