Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string only by the first occurrence of a delimiter

Tags:

julia

I would like to split a string by the first occurrence of ..

julia> a = "x1.y1.xyz22"
"x1.y1.xyz22"

julia> split(a,".")
3-element Array{SubString{String},1}:
 "x1"   
 "y1"   
 "xyz22"

How is it possible in Julia to split a string only once to get: "x1"
"y1.xyz22" ?

Thank you in advance,

like image 452
user977828 Avatar asked Jan 26 '20 21:01

user977828


People also ask

How do you split based on first occurrence?

Use the str. split() method with maxsplit set to 1 to split a string on the first occurrence, e.g. my_str. split('-', 1) . The split() method only performs a single split when the maxsplit argument is set to 1 .

How do you split a string only on the first instance of specified character?

To split a JavaScript string only on the first occurrence of a character, call the slice() method on the string, passing it the index of the character + 1 as a parameter. The slice method will return the portion of the string after the first occurrence of the character.

How do I split a string with first space?

Using the split() Method For example, if we put the limit as n (n >0), it means that the pattern will be applied at most n-1 times. Here, we'll be using space (” “) as a regular expression to split the String on the first occurrence of space.

How do you separate a string from a delimiter?

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.


2 Answers

Use the limit keyword.

For this kind of questions you can also efficiently use the inline documentation.. just type ?split (or any other function or type) in the console to retrieve a nice explanation of the function, their arguments and often usage examples. In this case:

help?> split
search: split splitext splitdir splitpath splitdrive rsplit splice! displaysize @specialize @nospecialize

  split(str::AbstractString, dlm; limit::Integer=0, keepempty::Bool=true)
  split(str::AbstractString; limit::Integer=0, keepempty::Bool=false)

  Split str into an array of substrings on occurrences of the delimiter(s) dlm. dlm can be any of the formats allowed by findnext's first argument (i.e. as a string, regular expression or a function), or as a single character or collection of characters.

  If dlm is omitted, it defaults to isspace.

  The optional keyword arguments are:

    •    limit: the maximum size of the result. limit=0 implies no maximum (default)

    •    keepempty: whether empty fields should be kept in the result. Default is false without a dlm argument, true with a dlm argument.

  See also rsplit.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> a = "Ma.rch"
  "Ma.rch"

  julia> split(a,".")
  2-element Array{SubString{String},1}:
   "Ma"
   "rch"

  ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  Splits an HyperRectangle into two along an axis at a given location.
like image 84
Antonello Avatar answered Oct 19 '22 02:10

Antonello


The limit keyword argument will set the maximum number of chunks in the result:

julia> split("abc.de.fg.hij.k", "."; limit=2) # split at most once
2-element Array{SubString{String},1}:
 "abc"        
 "de.fg.hij.k"

julia> split("abc.de.fg.hij.k", "."; limit=3) # split at most twice
3-element Array{SubString{String},1}:
 "abc"     
 "de"      
 "fg.hij.k"
like image 45
David Varela Avatar answered Oct 19 '22 03:10

David Varela