"함수 substrBefore()"의 두 판 사이의 차이

9번째 줄: 9번째 줄:
echo ${str%%l*}
echo ${str%%l*}
echo ${str%%x*}
echo ${str%%x*}
</syntaxhighlight>
==Go==
[[분류: Go]]
{{참고|Go stringBefore()}}
<syntaxhighlight lang='go' run>
package main
import (
"fmt"
"strings"
)
func stringBefore(haystack string, needle string) string {
pos := strings.Index(haystack, needle)
if pos == -1 {
return ""
}
return haystack[0:pos]
}
func main() {
fmt.Println(stringBefore("hello world", "l"))
fmt.Println(stringBefore("hello world", "ll"))
fmt.Println(stringBefore("hello world", "x"))
}
</syntaxhighlight>
</syntaxhighlight>



2022년 5월 19일 (목) 11:11 판

substr_before

1 Bash

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

2 Go

package main

import (
	"fmt"
	"strings"
)

func stringBefore(haystack string, needle string) string {
	pos := strings.Index(haystack, needle)
	if pos == -1 {
		return ""
	}
	return haystack[0:pos]
}
func main() {
	fmt.Println(stringBefore("hello world", "l"))
	fmt.Println(stringBefore("hello world", "ll"))
	fmt.Println(stringBefore("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 }}