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" }

7
  • 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..<upper] // "an example" } Commented Apr 16, 2017 at 1: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..<someString.endIndex)?.lowerBound { let substring = someString[lower..<upper] // "an example" } Commented Apr 16, 2017 at 2:03
  • BTW Range<String.Index> has no location property and range it is not a String static method Commented Apr 16, 2017 at 2:05
  • 1
    Are you sure you have added the import UIKit or import Foundation to your file? Commented Apr 16, 2017 at 2:13
  • I've wrote String by accident, meant to write NSString.range(of: "").location. Commented Apr 16, 2017 at 2:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.