ADVERTISEMENT
ADVERTISEMENT

Golang strings.TrimRight() Function with Examples

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

strings.TrimRight()

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

Syntax:

func TrimRight(str, cutset string) string

Parameter(s):

  • str : The string in which we have to remove trailing Unicode code points.
  • cutset : The string (a set of characters / Unicode code points) to be trimmed.

Return Value:

The return type of TrimRight() function is a string, it returns a string with all specified trailing Unicode code points removed.

Example 1:

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

package main

import (
	"fmt"
	"strings"
)

func main() {
	fmt.Println(strings.TrimRight("  Hello, world  ", " "))
	fmt.Println(strings.TrimRight("Hello,   world  ", " "))
	fmt.Println(strings.TrimRight("...Hello, world...", "."))
	fmt.Println(strings.TrimRight("###Hello, world!!!", "!"))
	fmt.Println(strings.TrimRight("###Hello, world!!!", "!!!"))
	fmt.Println(strings.TrimRight("###Hello, world!!!", "#!"))
}

Output:

  Hello, world
Hello,   world
...Hello, world
###Hello, world
###Hello, world
###Hello, world

Example 2:

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

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str string
	var result string

	str = "***Hi, there! How are you?"
	result = strings.TrimRight(str, "?")
	fmt.Println("Actual string :", str)
	fmt.Println("Trimmed string:", result)

	str = "Hi, how are you###"
	result = strings.TrimRight(str, "#")
	fmt.Println("Actual string :", str)
	fmt.Println("Trimmed string:", result)

	str = "¡¡¡Hello, world!!!"
	result = strings.TrimRight(str, "!")
	fmt.Println("Actual string :", str)
	fmt.Println("Trimmed string:", result)
}

Output:

Actual string : ***Hi, there! How are you?
Trimmed string: ***Hi, there! How are you
Actual string : Hi, how are you###
Trimmed string: Hi, how are you
Actual string : ¡¡¡Hello, world!!!
Trimmed string: ¡¡¡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.