ADVERTISEMENT
ADVERTISEMENT

Golang strings.TrimRightFunc() Function with Examples

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

strings.TrimRightFunc()

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

Syntax:

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

Parameter(s):

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

Return Value:

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

Example 1:

// Golang program to demonstrate the
// example of strings.TrimRightFunc() 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.TrimRightFunc("Hello, world.   ", f_IsSpace))
	fmt.Println(strings.TrimRightFunc("0891Hello, world123", f_IsNumber))
}

Output:

Hello, world.
0891Hello, world

Example 2:

// Golang program to demonstrate the
// example of strings.TrimRightFunc() 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.TrimRightFunc("  Hello, world.", f_Trim))
	fmt.Println(strings.TrimRightFunc("0891Hello, world123", f_Trim))
	fmt.Println(strings.TrimRightFunc("@@@Hello, world###", f_Trim))
	fmt.Println(strings.TrimRightFunc("...Hello, world,,,", f_Trim))
	fmt.Println(strings.TrimRightFunc("  Hello, world!!!", f_Trim))
	fmt.Println(strings.TrimRightFunc("+++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.