Julia: The value unicodeerror of Chinese string and its solution _unicode

Source: Internet
Author: User

In Julia, the value of a Unicode string may be accidentally a pit.
Thanks to @gnimuc, provide the following links that can be done as a reference.
Http://stackoverflow.com/questions/39501900/truncate-string-in-julia

I. Questions and Suggestions

Julia> STRs = "#我是中国人"
"#我是中国人"

julia> strs[1:1]
"#"

julia> Strs[2:2]
"I"

Julia > Strs[3:3]
ERROR:UnicodeError:invalid character index
 in GetIndex (:: String,:: Unitrange{int64}) at. \ strings\string.jl:130

julia> Strs[4:4]
ERROR:UnicodeError:invalid character index in
 getindex (:: String,:: Unitrange{int64}) at. \strings\string.jl:130

julia> Length (STRs)
6

Second, train of thought
If you get a value similar to Strs[3:3], what's the solution? String => character array first ...?

Julia> Data =collect (Take (Strs,length (STRs)))
6-element array{char,1}:
 ' # '
 i '
 is '
 in '
 ' country '
 people '
julia> data2= Vector{char} (STRs) # This is an alternative way 
6-element array{char,1}:
 ' # '
 i '
 ' is '
 in ' the '
 country '
 person '

julia> str =string (Data[2:4])
' [' I ', ' yes ', ' in '] '

julia> Str =string (data[4])
"in"

julia> str =string (DATA[2:4)) # recommended scheme, String PK string
"I am Medium"

julia> Str =string (Data[2:4] ...) # Alternative
' I'm in '

julia> str =string (Data[1:2])
"#我"

julia> str =string (data[2:2))
"I"

Third, the optimization plan

Now encapsulated as a custom function:

Julia> getstrs (strs::string) =collect (Take (Strs,length (STRs)))
getstrs (generic function with 1 method)

Julia> getindexstrs (strs::string,n,m) =string (Getstrs (STRs) [n:m]) # String!
Getindexstrs (Generic function with 1 method)

julia> getindexstrs (strs::string,n) =getindexstrs (strs,n,n)
getindexstrs (generic function with 2)

Efficiency optimization

function StringIndex (strs::string,n,m)
    if Isascii (STRs) return
        strs[n:m]
    else return
        getindexstrs ( STRS,N,M) End-end
stringindex (strs,n) =stringindex (strs,n,n);

Four, test

Julia> getindexstrs (strs,1,3)
"#我是"

julia> getindexstrs (strs,1,1)
"#"

julia> Getindexstrs (strs,3,3)
"is"

julia> getindexstrs (strs,2,4)
"I am Medium"

julia> getindexstrs ( strs,5) "
country"

julia> getindexstrs (strs,4)
"

julia> getindexstrs (strs,3)
" is "

Julia> StringIndex ("Q I am Chinese", 1,5) #isascii->false
"Q I am China"

julia> stringindex ("QABSCDSD", 2, 3) # Isascii->true
"AB"

There may be other ways (including form and performance may be different, such as Vector{char} (STRs)), but this approach is relatively simple and easy to understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.