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

잔글 (Jmnote님이 함수 string before() 문서를 함수 substrBefore() 문서로 이동했습니다)
 
(같은 사용자의 중간 판 하나는 보이지 않습니다)
13번째 줄: 13번째 줄:
==Go==
==Go==
[[분류: Go]]
[[분류: Go]]
{{참고|Go stringBefore()}}
{{참고|Go substrBefore()}}
<syntaxhighlight lang='go' run>
<syntaxhighlight lang='go' run>
package main
package main
22번째 줄: 22번째 줄:
)
)


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

2022년 5월 19일 (목) 14:12 기준 최신판

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 }}