TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang String Between, Before and After

Use the strings.Index func and string slices to get strings between, before and after other strings.
Between, before, after. We take substrings with slice syntax. And we search strings with strings.Index. When we want to get substrings based on adjacent values, we combine these tasks.
Some methods. We introduce between(), before and after funcs. To search from the end of a string with we use the strings.LastIndex func.
Example program. Here we introduce the three methods. The first argument is the string are we searching—this is the same argument style that strings.Index uses.

Result: Consider the main func. We declare a string literal that looks like some sort of data format.

And: We isolate parts of the string based on its surround parts. So we can parse a string like this without any custom string code.

Golang program that implements between, before and after package main import ( "fmt" "strings" ) func between(value string, a string, b string) string { // Get substring between two strings. posFirst := strings.Index(value, a) if posFirst == -1 { return "" } posLast := strings.Index(value, b) if posLast == -1 { return "" } posFirstAdjusted := posFirst + len(a) if posFirstAdjusted >= posLast { return "" } return value[posFirstAdjusted:posLast] } func before(value string, a string) string { // Get substring before a string. pos := strings.Index(value, a) if pos == -1 { return "" } return value[0:pos] } func after(value string, a string) string { // Get substring after a string. pos := strings.LastIndex(value, a) if pos == -1 { return "" } adjustedPos := pos + len(a) if adjustedPos >= len(value) { return "" } return value[adjustedPos:len(value)] } func main() { // Example string to parse. test := "DEFINE:A=TWO" // Test between func. fmt.Println(between(test, "DEFINE:", "=")) fmt.Println(between(test, ":", "=")) // Test before func. fmt.Println(before(test, ":")) fmt.Println(before(test, "=")) // Test after func. fmt.Println(after(test, ":")) fmt.Println(after(test, "DEFINE:")) fmt.Println(after(test, "=")) } Output A A DEFINE DEFINE:A A=TWO A=TWO TWO
Performance notes. In a parser, these methods might be slower than desired. Excess string searching can occur. They can be used as a starting point, and optimizations can be added later.
A review. In Go we can implement this kind of method in the same way as many other languages. This code is almost a line-by-line translation of a Python program. It works well in Go.SubstringIndex
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf