ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.