1. Characters & Strings
2. String function
3. Containers
1. Characters & Strings
julia> ch = ‘a’
‘a’
julia> typeof(ch)
Char
julia> str = “Hello world”
“Hello world”
julia> str[1]
‘H’
julia> typeof(str)
ASCIIString (constructor with 1 method)
julia> str[7:9] #index는 1부터 시작한다.
“wor”
julia> str[3:end]
“llo world”
$: String interpolation
Double quote 안에 있는 문자를 변수로 인식하도록 한다.
julia> a=2; b=3
3
julia> “a + b = $(a+b)”
“a + b = 5″
2. String function
http://docs.julialang.org/en/release-0.2/stdlib/base/#strings
length(s)
The number of characters in string s.
search(string, “substring”) = start:end such that string[start:end] == “substring”, or 0:-1 if unmatched.
search(string, ‘c’) = index such that string[index] == ‘c’, or 0 if unmatched.
replace(string, pat, r[, n])
Search for the given pattern pat, and replace each occurrence with r.
contains(haystack, needle)
Determine whether the second argument is a substring of the first.
beginswith(string, prefix)
Returns true if string starts with prefix.
uppercase(string)
Returns string with all characters converted to uppercase.
ex)
julia> hello_string = “Hello Julia\n”
“Hello Julia\n”
julia> length(hello_string)
12
julia> search(hello_string, “Ju”)
7:8
julia> replace(hello_string, “Hello”, “Hi”)
“Hi Julia\n”
julia> contains(hello_string, “Hello”)
true
julia> uppercase(hello_string)
“HELLO JULIA\n”
3. Containers
3.1 Array
julia> a = {} # Type이 ‘Any’인 1-D array
0-element Array{Any,1}
julia> a = Int[] # Type이 ‘Int’인 1-D array
0-element Array{Int64,1}
julia> b = [4 5 6]
1×3 Array{Int64,2}:
4 5 6
julia> b = [4,5,6]
3-element Array{Int64,1}:
4
5
6
3.2 Array Function
http://docs.julialang.org/en/release-0.2/stdlib/base/#arrays
push!(collection, item) → collection
Insert an item at the end of a collection.
pop!(collection) → item
Remove the last item in a collection and return it.
append!(collection, items) → collection.
Add the elements of items to the end of a collection. append!([1],[2,3]) => [1,2,3]
julia> a = Int64[]
0-element Array{Int64,1}
julia> push!(a,55)
julia> push!(a,32)
julia> push!(a,70)
julia> push!(a,46)
4-element Array{Int64,1}:
55
32
70
46
julia> sort(a) # ! 표기가 없는경우 실제 array 값을 바꾸진 않음
4-element Array{Int64,1}:
32
46
55
70
julia> show(a) # 실제 값은 바뀌지 않음
[55,32,70,46]
julia> sort!(a) # !이 있는경우 실제 array 값을 바꿈
4-element Array{Int64,1}:
32
46
55
70
3.3 Tuple
julia> tup = (1,2,3) # 값을 선언할 수 있지만 내용 변경 불가능한 자료구조
(1,2,3)
julia> tup[2]
2
julia> tup[2] = 3
ERROR: no method setindex!((Int64,Int64,Int64),Int64,Int64)
4.4 Dictionary
Dict()
Dict{K,V}() constructs a hashtable with keys of type K and values of type V. The literal syntax is {“A”=>1, “B”=>2} for a Dict{Any,Any}, or ["A"=>1, "B"=>2] for a Dict of inferred type.
keys(collection)
Return an iterator over all keys in a collection. collect(keys(d)) returns an array of keys.
values(collection)
Return an iterator over all values in a collection. collect(values(d)) returns an array of values.
haskey(collection, key)
Determine whether a collection has a mapping for a given key.
get(collection, key, default)
Return the value stored for the given key, or the given default value if no mapping for the key is present.
ex)
julia> empty_dict = Dict()
Dict{Any,Any}()
julia> filled_dict = ["one"=>1, "two"=>2, "three"=>3]
["three"=>3,"one"=>1,"two"=>2]
julia> values(filled_dict)
ValueIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
julia> keys(filled_dict)
KeyIterator{Dict{ASCIIString,Int64}}(["three"=>3,"one"=>1,"two"=>2])
julia> haskey(filled_dict,”one”)
true
julia> get( filled_dict, “one”, false)
1
julia> get( filled_dict, “four”, false)
false