# Parse a string using multiple delimiters

**URL:** <https://discourse.julialang.org/t/parse-a-string-using-multiple-delimiters/5012>\
**Category:** New to Julia\
**Created:** [July 22, 2017, 12:57am UTC](https://discourse.julialang.org/t/parse-a-string-using-multiple-delimiters/5012 "2017-07-22T00:57:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alfred](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alfred/32/1449_2.png) [@alfred](https://discourse.julialang.org/u/alfred)\
**Post date:** [July 22, 2017, 12:57am UTC](https://discourse.julialang.org/t/parse-a-string-using-multiple-delimiters/5012/1 "2017-07-22T00:57:39Z")

</div>

Hello and greetings,

I’m a new over here and I’ve started migrating some Python code to Julia, but I stuck at a dead end. Sorry to provide some .py lines in this forum, but I got some doubts about the best (fastest) way to do the same in Julia.

Functions explanation:  
**Executing the function parsertoken(“\_My input.string”, " \_,.", 2) will result “input”.**  
**Parsercount(“Julia=-rocks!”, " =-") will result 2.**

```julia
def parsertoken(istring, idelimiters, iposition):
    """
    Return a specific token of a given input string,
    considering its position and the provided delimiters

    :param istring: raw input string
    :param idelimiteres: delimiters to split the tokens
    :param iposition: position of the token
    :return: token
    """
    	vlist=''.join([s if s not in idelimiters else ' ' for s in istring]).split()
    	return vlist[vposition]

def parsercount(istring, idelimiters):
    """
    Return the number of tokens at the input string
    considering the delimiters provided

    :param istring: raw input string
    :param idelimiteres: delimiters to split the tokens
    :return: a list with all the tokens found
    """
    	vlist=''.join([s if s not in idelimiters else ' ' for s in istring]).split()
    	return len(vlist)-1

```

Given I really care about speed, in my Julia implementation, I’m thinking to change the former API, mainly because to get multiple tokens from a string, I have to split the string every single time.

Cheers

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 22, 2017, 2:27am UTC](https://discourse.julialang.org/t/parse-a-string-using-multiple-delimiters/5012/2 "2017-07-22T02:27:23Z")

</div>

> [@alfred](#):
>
> Given I really care about speed, in my Julia implementation, I’m thinking to change the former API, mainly because to get multiple tokens from a string, I have to split the string every single time.

Why not call

```julia
tokens = split("_My input.string", (' ','_',',','.'))

```

and then you can get whatever tokens you want from the resulting list?

If you need only a single token, e.g. the 3rd token, you could write a more efficient function to do that. It wouldn’t be too hard to adapt the `Base.split` function to pull out a specific token or set of tokens, since [Base.split is only 30 lines of code](https://github.com/JuliaLang/julia/blob/d9903b1f04c29ac0f434f959661a5d2d40ae0b35/base/strings/util.jl#L290-L310).
