Tuesday 9 February 2016

Julia: Strings


String is a sequence of characters enclosed in double (or) triple quotes.

julia> message="Hello World"; name="""Hari Krishna""";

julia> message
"Hello World"

julia> name
"Hari Krishna"


By using index notation, you can access a character from string.

julia> message
"Hello World"

julia> message[1]
'H'

julia> message[5]
'o'

julia> message[9]
'r'

Unlike languages C, C++, Java, index start from 1 in Julia.


Strings in Julia are immutable, i.e, you can’t change the string after creation.

julia> city="Bangalore"
"Bangalore"

julia> city[1]='b'
ERROR: MethodError: `setindex!` has no method matching setindex!(::ASCIIString, ::Char, ::Int64)

Keyword end
Keyword ‘end’ is used to represent the last index of a string.

julia> message[end]
'd'

julia> message[end-1]
'l'

julia> message[end-2]
'r'

julia> message[end-3]
'o'


If you tries to access an index < 1 (or) > end causes BoundsError.

julia> message[0]
ERROR: BoundsError: attempt to access 11-element Array{UInt8,1}:
 0x48
 0x65
 0x6c
 0x6c
 0x6f
 0x20
 0x57
 0x6f
 0x72
 0x6c
 0x64
  at index [0]
 in getindex at /Applications/Julia-0.4.1.app/Contents/Resources/julia/lib/julia/sys.dylib

julia> message[end+1]
ERROR: BoundsError: attempt to access 11-element Array{UInt8,1}:
 0x48
 0x65
 0x6c
 0x6c
 0x6f
 0x20
 0x57
 0x6f
 0x72
 0x6c
 0x64
  at index [12]
 in getindex at /Applications/Julia-0.4.1.app/Contents/Resources/julia/lib/julia/sys.dylib


Slicing
Slicing is used to get sub string of given string.

Example
Description
string[start:end]
Returns sub string from index start (included) to end index (included).
string[start:start]
Returns character at position start



julia> message
"Hello World"

julia> message[7:end]
"World"

julia> message[1:5]
"Hello"

julia> message[1:end]
"Hello World"

julia> message[5:5]
"o"

ASCIIString Vs UTF8String
If all the characters in string are valid ASCII characters, then the type of the sting is ASCIIString, else UTF8String.

julia> s = "hello"
"hello"

julia> typeof(s)
ASCIIString

julia> s = "\u2900 x \u2903 y"
"⤀ x ⤃ y"

julia> typeof(s)
UTF8String


Julia support for Unicode
Julia supports all Unicode characters. Unicode characters are specified by using \u (or) \U.

For example,
julia> s = "\u2900 x \U2903 y"
" x y"

All Non-ASCII characters are encoded using UTF-8 encoding. UTF-8 is variable length encoding which uses 8-bit code units. UTF-8 encodes all the Unicode characters using one to four 8-bit bytes. Unicode points with lower numeral number are encoded with fewer bytes.

julia> s = "\u2900x\u2903y"
"⤀x⤃y"

julia> s[1]
'⤀'

julia> s[2]
ERROR: UnicodeError: invalid character index
 in next at /Applications/Julia-0.4.1.app/Contents/Resources/julia/lib/julia/sys.dylib
 in getindex at strings/basic.jl:37

julia> s[3]
ERROR: UnicodeError: invalid character index
 in next at /Applications/Julia-0.4.1.app/Contents/Resources/julia/lib/julia/sys.dylib
 in getindex at strings/basic.jl:37

julia> s[4]
'x'

Observe the above code snippet, s[1] point to the symbol ‘’ which takes three bytes to represent, so s[2] and s[3] are invalid indexes for string s.

How to get next valid index
By using ‘nextind(str, i)’ method you can get next valid index after i.

julia> s = "\u2900x\u2903y"
"⤀x⤃y"

julia> nextind(s,1)
4

julia> nextind(s,4)
5

julia> nextind(s,5)
8

Get number of characters in string
‘length(s)’ return number of characters in string s.

julia> s = "\u2900x\u2903y"
"⤀x⤃y"

julia> length(s)
4

Get the last index of the string
By using endof(s) method, you can get the last index of string s.

julia> s = "\u2900x\u2903y"
"⤀x⤃y"

julia> endof(s)
8

Note:
Julia also provide support for UTF-16, UTF-32 encodings by using utf16() and utf32() respectively.

julia> s=utf16("Hello")
"Hello"

julia> typeof(s)
UTF16String

julia> s=utf32("Hello")
"Hello"

julia> typeof(s)
UTF32String

Concatenate strings
You concatenate strings using ‘*’ operator (or) by using string function.

julia> data="qwer"*"tyuio"*"asdf"
"qwertyuioasdf"

julia> data=string("abcd", "efgh", "ijlk")
"abcdefghijlk"

Comparison operators Vs string
You can compare strings using comparison operators.

julia> "abc"<"def"
true

julia> "abc"!="def"
true

julia> "abc"=="def"
false







Previous                                                 Next                                                 Home

No comments:

Post a Comment