Task: Text between




Go: package main

import (
    "fmt"
    "strings"
)

func textBetween(str, start, end string) string {
    if str == "" || start == "" || end == "" {
        return str
    }
    s := 0
    if start != "start" {
        s = strings.Index(str, start)
    }
    if s == -1 {
        return ""
    }
    si := 0
    if start != "start" {
        si = s + len(start)
    }
    e := len(str)
    if end != "end" {
        e = strings.Index(str[si:], end)
        if e == -1 {
            return str[si:]
        }
        e += si
    }
    return str[si:e]
}

func main() {
    texts := [9]string{
        "Hello Rosetta Code world",
        "Hello Rosetta Code world",
        "Hello Rosetta Code world",
        "</div><div style=\"chinese\">你好嗎</div>",
        "<text>Hello <span>Rosetta Code</span> world</text><table style=\"myTable\">",
        "<table style=\"myTable\"><tr><td>hello world</td></tr></table>",
        "The quick brown fox jumps over the lazy other fox",
        "One fish two fish red fish blue fish",
        "FooBarBazFooBuxQuux",
    }
    starts:= [9]string{
        "Hello ", "start", "Hello ", "<div style=\"chinese\">",
        "<text>", "<table>", "quick ", "fish ", "Foo",
    }
    ends := [9]string{
        " world", " world", "end", "</div>", "<table>",
        "</table>", " fox", " red", "Foo",
    }
    for i, text := range texts {
        fmt.Printf("Text: \"%s\"\n", text)
        fmt.Printf("Start delimiter: \"%s\"\n", starts[i])
        fmt.Printf("End delimiter: \"%s\"\n", ends[i])
        b := textBetween(text, starts[i], ends[i])
        fmt.Printf("Output: \"%s\"\n\n", b)
    }
}





VB: Option Explicit

Private Const STRING_START As String = "Start"
Private Const STRING_END As String = "End"

Sub Main()
Dim Text As String, First As String, Last As String, Output As String

    Text = "Hello Rosetta Code world"
        First = "Hello "
            Last = " world"
    Output = "1- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "Hello Rosetta Code world"
        First = "Start"
            Last = " world"
    Output = Output & "2- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "Hello Rosetta Code world"
        First = "Hello "
            Last = "End"
    Output = Output & "3- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "</div><div style=\""chinese\"">你好嗎</div>..."
        First = "<div style=\""chinese\"">"
            Last = "</div>"
    Output = Output & "4- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "<text>Hello <span>Rosetta Code</span> world</text><table style=\""myTable\"">"
        First = "<text>"
            Last = "<table>"
    Output = Output & "5- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "<table style=\""myTable\""><tr><td>hello world</td></tr></table>"
        First = "<table>"
            Last = "</table>"
    Output = Output & "6- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "The quick brown fox jumps over the lazy other fox"
        First = "quick "
            Last = " fox"
    Output = Output & "7- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "One fish two fish red fish blue fish"
        First = "fish "
            Last = " red"
    Output = Output & "8- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "FooBarBazFooBuxQuux"
        First = "Foo"
            Last = "Foo"
    Output = Output & "9- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "</div><div style=\""chinese\"">你好嗎..."
        First = "<div style=\""chinese\"">"
            Last = "</div>"
    Output = Output & "10- " & Text_Between(Text, First, Last) & vbCrLf

    Text = ""
        First = "Start"
            Last = "End"
    Output = Output & "11- " & Text_Between(Text, First, Last) & vbCrLf

    Text = "Hello Rosetta Code world"
        First = "Start"
            Last = "End"
    Output = Output & "12- " & Text_Between(Text, First, Last)

    Debug.Print Output
End Sub

Private Function Text_Between(T$, F$, L$) As String
Dim i As Long
    i = InStr(T, L) + 1
    Select Case True
        Case T = "", F = "", InStr(T, F) = 0 And F <> STRING_START
            Text_Between = ""
        Case F = STRING_START And L = STRING_END
            Text_Between = T
        Case F = STRING_START
            Text_Between = Mid(T, 1, InStr(T, L) - 1)
        Case L = STRING_END, InStr(T, L) = 0, InStr(T, L) < InStr(T, F) And InStr(i, T, L) = 0
            Text_Between = Mid(T, Len(F) + InStr(T, F))
        Case F = L
            Text_Between = Mid(T, Len(F) + InStr(T, F), InStr(i, T, F) - Len(F) - 1)
        Case Else
            Text_Between = Mid(T, Len(F) + InStr(T, F), InStr(InStr(T, F), T, L) - (Len(F) + InStr(T, F)))
    End Select
End Function