Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to treat String as Array/Vector in Julia

Tags:

julia

In Julia language, I want to use functions defined for Array{T,1} a.k.a. Vector{T} on a String, essentially treating it as Array{Char,1}.

An example of a function I would like to use:

julia> deleteat!("Hrello!",2)
ERROR: MethodError: no method matching deleteat!(::String, ::Int64)
Closest candidates are:
  deleteat!(::Array{T,1} where T, ::Integer) at array.jl:1177
  deleteat!(::Array{T,1} where T, ::Any) at array.jl:1214
  deleteat!(::BitArray{1}, ::Integer) at bitarray.jl:901
  ...
Stacktrace:
 [1] top-level scope at none:0

julia> deleteat!(['H','r','e','l','l','o','!'], 2)
6-element Array{Char,1}:
 'H'
 'e'
 'l'
 'l'
 'o'
 '!'

To be clear, I would like to start with a String and end up with a String, but use Array {Char,1} operations to alter the String.

like image 901
dukereg Avatar asked Dec 28 '18 23:12

dukereg


People also ask

How do you make a vector in Julia?

Creating a Vector A Vector in Julia can be created with the use of a pre-defined keyword Vector() or by simply writing Vector elements within square brackets([]). There are different ways of creating Vector. vector_name = [value1, value2, value3,..] or vector_name = Vector{Datatype}([value1, value2, value3,..])

How do you make an array in Julia?

A 1D array can be created by simply writing array elements within square brackets separated by commas(, ) or semicolon(;). A 2D array can be created by writing a set of elements without commas and then separating that set with another set of values by a semicolon.

How do you change a string on Julia?

The replace() is an inbuilt function in julia that is used to replace a word or character with the specified string or character. Parameters: s::AbstractString: Specified string. pattern=>Word: Pattern is searched from the given sentence and then that pattern is replaced with the word.

How do you split a string in Julia?

Splitting string into array of substrings in Julia – split() and rsplit() Method. The split() is an inbuilt function in julia which is used to split a specified string into an array of substrings on occurrences of the specified delimiter(s).


Video Answer


1 Answers

In Julia, one should always try collect firstly for getting a Vector from something else.

julia> deleteat!(collect("Hrello!"), 2)
6-element Array{Char,1}:
 'H'
 'e'
 'l'
 'l'
 'o'
 '!'
like image 110
Gnimuc Avatar answered Oct 28 '22 07:10

Gnimuc