How to extract a substring near the Nth occurrence of a string or character in a string

A Scala substring example: I ran into a situation today where I wanted to get a string after the Nth occurrence of another string, in this case after the 6th occurrence of a “:” character. There are probably many ways to determine the Nth occurrence of a string in another string, but as a quick example, this is what I did.

First, I started with this string:

val s = """a:2:{s:3:"alt";s:35:"The Fairview Inn, Talkeetna, Alaska";s:5:"title";s:0:"";}"""

Within that string I wanted to extract the “The Fairview Inn, Talkeetna, Alaska” string. The way I approached this problem was to split the input string on the “:” character, giving me this:

scala> s.split(":")
res0: Array[String] = Array(a, 2, {s, 3, "alt";s, 35, "The Fairview Inn, Talkeetna, Alaska";s, 5, "title";s, 0, "";})

Then I saw that the substring I wanted was in the seventh position of the resulting array:

scala> s.split(":")(6)
res1: String = "The Fairview Inn, Talkeetna, Alaska";s

There are some extra junk characters on the end of that string, so I used dropRight:

scala> s.split(":")(6).dropRight(3)
res2: String = "The Fairview Inn, Talkeetna, Alaska

Then I noticed there was a double-quote at the beginning of the string, so I used drop on it:

scala> s.split(":")(6).drop(1).dropRight(3)
res3: String = The Fairview Inn, Talkeetna, Alaska

Of course there are other ways to solve this problem, but if you want to get a substring somewhere around the Nth occurrence of a string or character in another string, I hope this example is helpful.

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Enter the characters shown in the image.