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:

Perl substr example - How to extract a substring from a string

Perl substring FAQ: How do I extract a substring from a string?

Solution: Use the Perl substr function. The general substr syntax looks like this:

$my_substring = substr($full_string, $start_pos, $length);

Perl substring example (substr)

A more specific Perl substring example is shown in the following code, where I extract the $firstname and $lastname strings from the $name string:

Perl substring - How to search for one string in another string

Perl substring FAQ: Can you demonstrate some Perl substring examples?

As a language, Perl is very good at text processing, including dealing with strings and substrings. In this article we'll take a look at some Perl substring manipulations we can perform.

For the purposes of our Perl substring tutorial, let's assume that we have a string defined like this:

A Ruby substring example

Ruby substring FAQ: I can't find a Ruby substring method, how can I tell if one Ruby string contains another string?

Solution: I expected the Ruby String class to include a method named substring, but it doesn't. My personal disappointment aside, it does have a method named include? that works the way I expected it to. Here's a simple example, using the irbenvironment to perform a few tests.

AppleScript time example: The current time as hours and minutes

AppleScript time subroutine FAQ: Can you share an example of an AppleScript subroutine (function) that returns te current time?

Here's an example AppleScript subroutine (also known as a function or method) that returns the current time as hours and minutes, along with the AM/PM modifier. For the purposes of creating an alarm clock AppleScript program, I didn't like the default format of the AppleScript current date command, so I use this method to extract the current time information I want.