Golang strings.TrimLeftFunc() Function with Examples

Golang | strings.TrimLeftFunc() Function: Here, we are going to learn about the TrimLeftFunc() function of strings package with its usages, syntax, and examples.
Submitted by IncludeHelp, on August 23, 2021

strings.TrimLeftFunc()

The TrimLeftFunc() function is an inbuilt function of strings package which is used to get a string with all leading Unicode code points c satisfying f(c) removed. It accepts two parameters – a string and function, and returns trimmed string.

Syntax:

func TrimLeftFunc(str string, f func(rune) bool) string

Parameter(s):

  • str : The string in which we have to remove leading Unicode code points.
  • f : The function to be checked the given condition for leading Unicode code point.

Return Value:

The return type of TrimLeftFunc() function is a string, it returns a string with all leading Unicode code points c satisfying f(c) removed.

Example 1:

// Golang program to demonstrate the
// example of strings.TrimLeftFunc() Function

package main

import (
	"fmt"
	"strings"
	"unicode"
)

func main() {
	// To check space
	f_IsSpace := func(r rune) bool {
		return unicode.IsSpace(r)
	}

	// to check number
	f_IsNumber := func(r rune) bool {
		return unicode.IsNumber(r)
	}
	fmt.Println(strings.TrimLeftFunc("  Hello, world.", f_IsSpace))
	fmt.Println(strings.TrimLeftFunc("0891Hello, world123", f_IsNumber))
}

Output:

Hello, world.
Hello, world123

Example 2:

// Golang program to demonstrate the
// example of strings.TrimLeftFunc() Function

package main

import (
	"fmt"
	"strings"
	"unicode"
)

func main() {
	// To check all type of Unicode code points
	// except letters and numbers
	f_Trim := func(r rune) bool {
		return !unicode.IsLetter(r) && !unicode.IsNumber(r)
	}

	fmt.Println(strings.TrimLeftFunc("  Hello, world.", f_Trim))
	fmt.Println(strings.TrimLeftFunc("0891Hello, world123", f_Trim))
	fmt.Println(strings.TrimLeftFunc("@@@Hello, world###", f_Trim))
	fmt.Println(strings.TrimLeftFunc("...Hello, world,,,", f_Trim))
	fmt.Println(strings.TrimLeftFunc("  Hello, world!!!", f_Trim))
	fmt.Println(strings.TrimLeftFunc("+++Hello, world===", f_Trim))
}

Output:

Hello, world.
0891Hello, world123
Hello, world###
Hello, world,,,
Hello, world!!!
Hello, world===

Golang strings Package Functions »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.