How to remove the last occurrence of a character from a string in Go

1 Answer

0 votes
package main

import (
	"fmt"
	"strings"
)

func main() {
	str := "go scala c# c python c++ java rust"

	lastIndex := strings.LastIndex(str, "c")
	if lastIndex != -1 {
		str = str[:lastIndex] + str[lastIndex + 1:]
	}

	fmt.Println(str)
}



/*
run:

go scala c# c python ++ java rust

*/

 



answered Sep 8, 2024 by avibootz

Related questions

...