Golang strings.CutSuffix Function

The strings.CutSuffix function in Golang is part of the strings package and is used to check if a string ends with a specified suffix and, if so, removes the suffix from the string. It returns the string without the suffix and a boolean indicating whether the suffix was found. This function is useful for scenarios where you need to trim specific endings from strings, such as file extensions, URL parameters, or other standardized suffixes.

Table of Contents

  1. Introduction
  2. CutSuffix Function Syntax
  3. Examples
    • Basic Usage
    • Handling File Extensions
  4. Real-World Use Case
  5. Conclusion

Introduction

The strings.CutSuffix function allows you to remove a suffix from a string if it exists. This can be particularly useful for handling file paths, URLs, or any other strings where a known suffix needs to be trimmed for further processing.

CutSuffix Function Syntax

The syntax for the strings.CutSuffix function is as follows:

func CutSuffix(s, suffix string) (before string, found bool)

Parameters:

  • s: The main string to be checked and modified.
  • suffix: The suffix to check for and remove from the main string.

Returns:

  • before: The string without the suffix, or the original string if the suffix is not found.
  • found: A boolean value indicating whether the suffix was found and removed.

Examples

Basic Usage

This example demonstrates how to use the strings.CutSuffix function to remove a suffix from a string.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	// Define the main string
	str := "example.txt"

	// Define the suffix to be removed
	suffix := ".txt"

	// Use strings.CutSuffix to remove the suffix from the string
	before, found := strings.CutSuffix(str, suffix)

	// Print the result
	if found {
		fmt.Printf("String after suffix removal: %s\n", before)
	} else {
		fmt.Println("Suffix not found in the string.")
	}
}

Output:

String after suffix removal: example

Handling File Extensions

You can use strings.CutSuffix to handle file paths by removing specific file extensions.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	// Define a file name
	fileName := "document.pdf"

	// Define the suffix to be removed
	suffix := ".pdf"

	// Use strings.CutSuffix to remove the suffix from the file name
	before, found := strings.CutSuffix(fileName, suffix)

	// Print the result
	if found {
		fmt.Printf("File name after suffix removal: %s\n", before)
	} else {
		fmt.Println("Suffix not found in the file name.")
	}
}

Output:

File name after suffix removal: document

Real-World Use Case

Removing URL Parameters

In real-world applications, strings.CutSuffix can be used to remove trailing URL parameters or fragments from a URL.

Example

package main

import (
	"fmt"
	"strings"
)

func main() {
	// Define a URL
	url := "https://example.com/page.html#section"

	// Define the suffix to be removed
	suffix := "#section"

	// Use strings.CutSuffix to remove the suffix from the URL
	before, found := strings.CutSuffix(url, suffix)

	// Print the result
	if found {
		fmt.Printf("URL after suffix removal: %s\n", before)
	} else {
		fmt.Println("Suffix not found in the URL.")
	}
}

Output:

URL after suffix removal: https://example.com/page.html

Conclusion

The strings.CutSuffix function in Go provides a straightforward way to check for and remove suffixes from strings. It is particularly useful for processing strings with known endings, such as file extensions or URL fragments. By using strings.CutSuffix, you can efficiently manipulate strings in your Go applications and prepare them for further processing.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top