The Wayback Machine - https://web.archive.org/web/20240810061236/https://www.geeksforgeeks.org/strings-map-function-in-golang-with-examples/
Open In App

strings.Map() Function in Golang With Examples

Last Updated : 22 Dec, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

strings.Map() Function in Golang is used to return a copy of the string given string with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the string with no replacement.

So whenever the user wants to make changes in a certain string, he can make use of the strings.Map() function. It replaces the character of a string with a user-desired character. If we need to mask certain characters including digits and spaces, this function can be used.

Syntax:

func Map(mapping func(rune) rune, s string) string

The mapping func(rune) rune parameter defines the character with which the original character needs to be replaced while the s parameter defines the original string inputted by the user.

Example 1:




// Golang program to illustrate 
// the strings.Map() Function
package main
   
import (
    "fmt";
    "strings"
)
   
func main() {
    modified := func(r rune) rune {
        if r == 'e' {
            return '@'
        }
        return r
    }
   
    input:= "Hello, Welcome to GeeksforGeeks"
    fmt.Println(input)
   
    // using the function
    result := strings.Map(modified, input)
    fmt.Println(result)
}


Output:

Hello, Welcome to GeeksforGeeks
H@llo, W@lcom@ to G@@ksforG@@ks

Explanation: In the above example, we have used strings.Map() function to modify a string. We have defined a variable named “modified” that replaces character ‘e’ in the string with the symbol ‘@’. Another variable named “input” takes an input string that needs transformation.

Example 2: The strings.Map() function can also be used to remove the spaces between the words of a string.




// Golang program to illustrate 
// the strings.Map() Function
package main
   
import (
    "fmt";
    "strings"
)
   
func main() {
    transformed := func(r rune) rune {
           
        if r == ' ' {
            return 0
        }
        return r
    }
   
    input:= "GeeksforGeeks is a computer science portal."
    fmt.Println(input)
   
    // using the function
    output := strings.Map(transformed, input)
    fmt.Println(output)
}


Output:

GeeksforGeeks is a computer science portal.
GeeksforGeeksisacomputerscienceportal.

Explanation: In the above example, we have used strings.Map() function to remove the spaces in between a string. We have defined a variable named “transformed” that eliminates space ‘ ‘ in the string. This is done by returning 0 at the place of spaces. Another variable named “input” takes an input string whose in-between spaces need to be removed.



Similar Reads

How to copy a map to another map in Golang?
Maps in Golang is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update, or delete with the help of keys. In Map, you can copy a map to another map using the for loop provided by the Go language. In for loop, we fetch the index value 1 by 1 with the element and assign i
2 min read
strings.Contains Function in Golang with Examples
strings.Contains Function in Golang is used to check the given letters present in the given string or not. If the letter is present in the given string, then it will return true, otherwise, return false. Syntax: func Contains(str, substr string) bool Here, str is the original string and substr is the string that you want to check. Let us discuss th
2 min read
strings.ContainsRune() Function in Golang with Examples
The Unicode, which is a superset of ASCII and contains all the characters present in the world's writing system, is the character set that's being followed right now. Each character in the Unicode system is uniquely identified by a Unicode code point, which is referred to as runes in Golang. To know more about runes please go through the article Ru
2 min read
strings.Compare() Function in Golang with Examples
The Compare() function is an inbuilt function in the Golang programming language which is used to compare two strings. It is used to compare two strings in lexicographical order (order in which the words are arranged alphabetically, similar to the way we search words in dictionary) or to find out if the strings are equal or not. It returns an integ
2 min read
strings.Index() Function in Golang With Examples
strings.Index() Function in Golang is used to get the first instance of a specified substring. If the substring is not found, then this method will return -1. Syntax: func Index(str, sbstr string) int Here, str is the original string and sbstr is a string whose we want to find index value. Example 1: // Go program to illustrate the // String Index(
2 min read
strings.LastIndexAny() Function in Golang With Examples
strings.LastIndexAny() Function in the Golang is used to find the index of the last instance of any Unicode code point from chars in a given string. If the Unicode code point from chars is not found then it returns -1. Thus, this function returns an integer value. The index is counted taking zero as the starting index of the string. Syntax: func La
2 min read
strings.LastIndex() Function in Golang With Examples
strings.LastIndex() function in the Golang returns the starting index of the occurrence of the last instance of a substring in a given string. If the substring is not found then it returns -1. Thus, this function returns an integer value. The index is counted taking zero as the starting index of the string. Syntax: func LastIndex(str, substring str
2 min read
strings.SplitN() Function in Golang with Examples
strings.SplitN() Function() is a string manipulation function in Go language. It is used to split a given string into substrings separated by a separator. This function returns the slices of all substrings between those separators. Syntax: func SplitN(s, sep string, n int) []string Here, s is the string and sep is the separator. If s does not conta
3 min read
strings.Fields() Function in Golang With Examples
strings.Fields() Function in Golang is used to splits the given string around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of substrings of str or an empty slice if str contains only white space. Syntax: func Fields(str string) []string Returns: A slice of substrings of str or an
2 min read
strings.Title() Function in Golang With Examples
strings.Title() Function returns a copy of string s with all Unicode letters of string whose begin words mapped to their Unicode title case. This method belongs to the strings package. Syntax: func Title(s string) string It returns the string. Example 1: // Golang program to illustrate the strings.Title() function package main import ( "fmt
1 min read
Article Tags :
three90RightbarBannerImg