Go substrBefore()

1 개요[ | ]

Go substrBefore()
Go stringBefore()
Go
Copy
package main

import (
	"fmt"
	"strings"
)

func substrBefore(haystack string, needle string) string {
	pos := strings.Index(haystack, needle)
	if pos == -1 {
		return ""
	}
	return haystack[:pos]
}
func main() {
	fmt.Println(substrBefore("hello world", "l"))  // he
	fmt.Println(substrBefore("hello world", "ll")) // he
	fmt.Println(substrBefore("hello world", "x"))  //
}
Loading

2 같이 보기[ | ]