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

 
(사용자 2명의 중간 판 8개는 보이지 않습니다)
4번째 줄: 4번째 줄:
==Bash==
==Bash==
[[category: Bash]]
[[category: Bash]]
<source lang='bash'>
<syntaxhighlight lang='bash' run>
str='hello world'
str='hello world'
echo ${str%%ll*}
echo ${str%%ll*}
# he
echo ${str%%l*}
echo ${str%%l*}
# he
echo ${str%%x*}
echo ${str%%x*}
# hello world
</syntaxhighlight>
</source>
 
==Go==
[[분류: Go]]
{{참고|Go substrBefore()}}
<syntaxhighlight lang='go' run>
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"))  //
}
</syntaxhighlight>


==PHP==
==PHP==
[[category:PHP]]
[[category:PHP]]
<source lang='php'>
<syntaxhighlight lang='php' run>
echo strstr('hello world', 'll', true);
var_dump( strstr('hello world', 'll', true) );
echo strstr('hello world', 'l', true);
var_dump( strstr('hello world', 'l', true) );
echo strstr('hello world', 'x', true); // bool(false)
var_dump( strstr('hello world', 'x', true) );
# he
</syntaxhighlight>
# he
<syntaxhighlight lang='php' run>
#
</source>
<source lang='php'>
function substr_before($needle, $haystack) { return strstr($haystack, $needle, true); }
function substr_before($needle, $haystack) { return strstr($haystack, $needle, true); }
 
var_dump( substr_before('ll', 'hello world') );
echo substr_before('ll', 'hello world');
var_dump( substr_before('l', 'hello world') );
echo substr_before('l', 'hello world');
var_dump( substr_before('x', 'hello world') ); // bool(false)
echo substr_before('x', 'hello world'); // bool(false)
</syntaxhighlight>
# he
# he
#
</source>


==같이 보기==
==같이 보기==

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