함수 substrBefore()

(함수 substr before에서 넘어옴)
substr_before

1 Bash[ | ]

str='hello world'
echo ${str%%ll*}
echo ${str%%l*}
echo ${str%%x*}

2 Go[ | ]

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"))  //
}

3 PHP[ | ]

var_dump( strstr('hello world', 'll', true) );
var_dump( strstr('hello world', 'l', true) );
var_dump( strstr('hello world', 'x', true) );
function substr_before($needle, $haystack) { return strstr($haystack, $needle, true); }
var_dump( substr_before('ll', 'hello world') );
var_dump( substr_before('l', 'hello world') );
var_dump( substr_before('x', 'hello world') ); // bool(false)

4 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}