How to split a string at the last occurence of a sequence

Target: A string with a built-in separator shall be split in an int and another string. In the case that the separator sequence '###' occurs more than once, the string shall always be spliced at the last '###'.

Is there an operator like string.lastIndexOf("###"), like in C#?

This is how my parser looks like:

func parseTuple(from string: String) -> (String, Int)? { let parsedString = string.components(separatedBy: "###") if let tupleString = String(parsedString[0]), let tupleInt = Int(parsedString[1]) { return (tupleString, tupleInt) } else { return nil } }


The range(of:...) method of String has a .backwards option to find the last occurrence of a string. Then substring(to:) and substring(from:) can be used with the lower/upper bound of that range to extract the parts of the string preceding/following the separator:

func parseTuple(from string: String) -> (String, Int)? { if let theRange = string.range(of: "###", options: .backwards), let i = Int(string.substring(from: theRange.upperBound)) { return (string.substring(to: theRange.lowerBound), i) } else { return nil } }


if let tuple = parseTuple(from: "Connect###Four###Player###7") { print(tuple) // ("Connect###Four###Player", 7) } <hr>

<strong>Swift 4 update:</strong>

func parseTuple(from string: String) -> (String, Int)? { if let theRange = string.range(of: "###", options: .backwards), let i = Int(string[theRange.upperBound...]) { return (String(string[...theRange.lowerBound]), i) } else { return nil } }


let input = "Connect###Four###Player###7" let seperator = "###" // split components var components = input.components(separatedBy: seperator) // remove the last component ... components = Array(components.dropLast()) // ... and re-join the remaining ones let output = components.joined(separator: seperator) print(output)




  • Split string into substring with component separated by string swift
  • Get the initials from a name and limit it to 2 initials
  • How to play multiple Audio Files simultaneously using AVPlayer?
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • drawing random circles, storing their coorindates in an array
  • How to pass EC named curve list used by SSL_CTX?
  • How to read piped content in C?
  • Regarding starting the threads on a condition
  • Simple linked list-C
  • SQLite connection strategies
  • How to calculate the camera position from Vuforia GL matrix?
  • How do I include a SWC in an AS2 Flash project?
  • Checking if an array in C is symmetric
  • Reduction and collapse clauses in OMP have some confusing points
  • Appending Character to Character Array In C
  • Zurb Foundation _global.scss meta styles for js?
  • OOP Javascript - Is “get property” method necessary?
  • GridView breaks while scrolling
  • copying resource to sdcard gives a damaged file in android
  • Database structure design with variable amounts of fields
  • how to adjust image in a panel in Java swing?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Change JButton Shape while respecting Look And Feel
  • Problems to linebreak with an int in JLabel
  • How reduce the height of an mschart by breaking up the y-axis
  • output of program is not same as passed argument
  • Updating server-side rendering client-side
  • MySQL WHERE-condition in procedure ignored
  • Rearranging Cells in UITableView Bug & Saving Changes
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • apache spark aggregate function using min value
  • Sorting a 2D array using the second column C++
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal