ADVERTISEMENT
ADVERTISEMENT

Golang strings.TrimSuffix() Function with Examples

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

strings.TrimSuffix()

The TrimSuffix() function is an inbuilt function of strings package which is used to get a string without the provided leading suffix string. It accepts two parameters – a string and a suffix string and returns the string after trimming the suffix.

If the string doesn't end with the given suffix, the string is returned unchanged.

Syntax:

func TrimSuffix(str, suffix string) string

Parameter(s):

  • str : The string in which we have to remove suffix string
  • suffix : The suffix string to be trimmed.

Return Value:

The return type of TrimSuffix() function is a string, it returns a string without the provided leading suffix string.

Example 1:

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

package main

import (
	"fmt"
	"strings"
)

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

Output:

  Hello
...Hello, world
###Hello
Hi! World
###Hello, world!!!

Example 2:

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

package main

import (
	"fmt"
	"strings"
)

func main() {
	var str string
	var result string

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

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

	str = "¡¡¡Hello, world!!!"
	result = strings.TrimSuffix(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 
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.