TheDeveloperBlog.com

Home | Contact Us

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

<< Back to GO

Golang Trim, TrimSpace and TrimFunc Examples

Use the Trim func to remove spaces and other runes from the left and right of strings.
Trim. Often a string has leading or trailing characters on it (like whitespace) that we do not want. These can be stripped or trimmed away.Strings
In Golang we can invoke the Trim func, or one of its many variants like TrimPrefix. We can remove more than just spaces—any rune, or range of runes, can be trimmed.
Trim, TrimLeft, TrimRight. Let us begin with the simplest Trim methods. We can call Trim with 2 arguments—a string containing characters to remove is the second argument.

Here: We just remove spaces by passing a 1-char space string. The TrimSpace func could be used instead of Trim.

Golang program that uses Trim, TrimLeft, TrimRight package main import ( "fmt" "strings" ) func main() { test := " hello " // Remove spaces with Trim, TrimLeft, TrimRight. result1 := strings.Trim(test, " ") result2 := strings.TrimLeft(test, " ") result3 := strings.TrimRight(test, " ") fmt.Println("{" + result1 + "}") fmt.Println("{" + result2 + "}") fmt.Println("{" + result3 + "}") } Output {hello} {hello } { hello}
TrimSpace. For just spaces and newlines, we can use TrimSpace. This is a better choice if a variety of whitespace runes may be present on the start or end of a string.

Tip: The whitespace can be in any order—spaces can be before or after newlines.

Golang program that uses TrimSpace package main import ( "fmt" "strings" ) func main() { test := " hello \n" // Remove leading and training whitespace chars. result1 := strings.TrimSpace(test) fmt.Println("{" + result1 + "}") } Output {hello}
Prefix, suffix. In Golang we find many "prefix" and "suffix" funcs. These act on the starts and ends of strings. A prefix or suffix that we want to remove is passed as the second argument.

Important: For TrimPrefix and TrimSuffix, the string is left unchanged if the argument cannot be found.

Golang program that uses TrimPrefix, TrimSuffix package main import ( "fmt" "strings" ) func main() { test1 := "https://www.example.com/" fmt.Println(":::TRIM PREFIX:::") result1 := strings.TrimPrefix(test1, "https://") fmt.Println(result1) fmt.Println(":::TRIM SUFFIX:::") result2 := strings.TrimSuffix(test1, ".com/") fmt.Println(result2) // If we try to trim something that is not there, nothing happens. result3 := strings.TrimSuffix(test1, ".net/") fmt.Println(result3) } Output :::TRIM PREFIX::: www.example.com/ :::TRIM SUFFIX::: https://www.example https://www.example.com/
TrimFunc. We can use TrimFunc and pass a func that receives a rune (and returns a bool) as the second argument. The func can test the rune's value and return true if it should be trimmed.

Here: We use the ASCII range for a digit value in the func. If the ASCII value represents a digit between 0 and 9, we remove it.

And: The result has the leading and trailing digits stripped. Only the Hello remains.

Golang program that uses TrimFunc package main import ( "fmt" "strings" ) func main() { source := "123Hello4567" // Use TrimFunc with a rune-testing function. // ... If rune is a digit in ASCII return true. // These chars are removed. result := strings.TrimFunc(source, func(c rune) bool { return c >= 48 && c <= 57 }) fmt.Println("{" + result + "}") } Output {Hello}
A review. Trimming strings is important in Golang programs. We access Trim funcs in the "strings" package—be sure to import "strings" in your program.
© 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