julia:字符串基本知识

# strings are defined with double quotes
s1 = "The quick brown fox jumps over the lazy dog α,β,γ"
println(s1)

# println: adds a new line to the end of output
# print: can be used if u dont want adds a new line
print("this")
print(" add")  # "add"紧接着"this"输出
print(" that. \n")

# chars are defined with single quotes
c1 = 'a'
println(c1)

# the ascii value of a char can be found with Int():
println(c1, " ascii value = ", Int(c1))
#> a ascii value = 97
println("Int('α') == ", Int('α'))

# so be aware that
println(Int('1') == 1)
#> false

# strings can be converted to upper case or lower case:
s1_caps = uppercase(s1)
s1_lower = lowercase(s1)
println(s1_caps, "\n", s1_lower)

# sub strings can be indexed like arrays:
# show prints the raw value
show(s1[11]); println()
#> 'b'

# or sub strings can be created:
show(s1[1:10]); println()
#> "The quick "

# end is used for the end of the array or string
show(s1[end-10:end]); println()
#> "dog α, β, γ"

# julia allows string Interpolation:
a = "welcome"
b = "julia"
println("$a to $b")
#> welocme to julia

# strings can also be concatenated using * operator
# using * instead of +
s2 = "this" * " and" * " that"
println(s2)
#> this and that

# as well as the string function
s3 = string("this", " and", " that")
println(s3)
#> this and that

猜你喜欢

转载自blog.csdn.net/chd_lkl/article/details/82823958