2

I am new to Swift and I was looking for some help, please.

I have a String

let someString = "Here is an example of string"

I want to get is an example part of it. In Java I can do something like:

String someString = "Here is an example of string"    
String s = someString.substring(someString.indexOf("is "), someString.indexOf(" of"));

I was wondering if there is anything similar in Swift to Java's String.indexOf("some string") in order to find an index of the element of String. I've looked around and couldn't find anything. What I need it for is to find startIndex and endIndexes of a string that I am going to substring. I understand that it can be done easier with something like string.endIndex.advanceBy(-9), but string I am looking to go through is a couple of paragraphs long.

Found the solution: NSString.range(of: "string").location returns the index I've been looking for in case anyone wondering

UPDATE

Leo's answer works as well for anyone looking for an alternative:

Note that you should make sure to only search the range " of" string after the occurrence to the "is ". if let lower = someString.range(of: "is ")?.upperBound, let upper = someString.range(of: " of", range: lower..<someString.endIndex)?.lowerBound { let substring = someString[lower..<upper] // "an example" }

Ademir Gotov
  • 179
  • 1
  • 14
  • if you need the substring between the two substrings you just need to get the proper lower and upper indexes `if let lower = someString.range(of: "is ")?.upperBound, let upper = someString.range(of: " of")?.lowerBound { let substring = someString[lower.. – Leo Dabus Apr 16 '17 at 01:47
  • Note that you should make sure to only search the range " of" string after the occurrence to the "is ". `if let lower = someString.range(of: "is ")?.upperBound, let upper = someString.range(of: " of", range: lower.. – Leo Dabus Apr 16 '17 at 02:03
  • BTW `Range` has no location property and range it is not a String static method – Leo Dabus Apr 16 '17 at 02:05
  • 1
    Are you sure you have added the `import UIKit` or `import Foundation` to your file? – Leo Dabus Apr 16 '17 at 02:13
  • I've wrote String by accident, meant to write NSString.range(of: "").location. – Ademir Gotov Apr 16 '17 at 02:16
  • Don't use NSString. Swift native type is String – Leo Dabus Apr 16 '17 at 02:17
  • By the way, thank you for the solution you provided, it works as well – Ademir Gotov Apr 16 '17 at 02:17

1 Answers1

1

You can achieve something like this with the range method of Swifts String. It will return the range of the given string within the search string.

In Swift the start and end index is the lowerBound and upperBound of the resulting range.

Here is an example based on your example:

import Foundation

let text = "Here is an example of string"

if let textRange = text.range(of: "is an example") {
    textRange.lowerBound // startIndex
    textRange.upperBound // endIndex
    let otherText = text.substring(with: textRange) // result = "is an example"

    print("\(otherText)")
}

You can put this in a Playground and play around with it.

Jens
  • 20,533
  • 11
  • 60
  • 86
  • Thanks for the reply, but the problem is that I know the string it starts with and the string it ends with but the text in between may be different. I might not have been clear. So for example string may be "Here is the example of string" I know that it starts with "is" and end at the beginning of "of" but string in between them could be anything, that why I am trying to find exact startIndex and endIndex – Ademir Gotov Apr 16 '17 at 01:19