"함수 explode with whitespaces()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 3개는 보이지 않습니다)
4번째 줄: 4번째 줄:
==C==
==C==
[[분류: C]]
[[분류: C]]
<source lang='c'>
<syntaxhighlight lang='c'>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
11번째 줄: 11번째 줄:
#define NUMBER_OF_WORDS 200
#define NUMBER_OF_WORDS 200


int main () {
int main() {
     char *words[NUMBER_OF_WORDS];
     char *words[NUMBER_OF_WORDS];
     char seps[] = " \t\n";
     char seps[] = " \t\n";
   
     char str[] = " This\tis    my\nC-14 Impaler gauss rifle! ";
     char str[80] = "Hello World!";


     int count = 0;
     int count = 0;
32번째 줄: 31번째 줄:
     return 0;
     return 0;
}
}
// 0: [Hello]
// 0: [This]
// 1: [World!]
// 1: [is]
</source>
// 2: [my]
// 3: [C-14]
// 4: [Impaler]
// 5: [gauss]
// 6: [rifle!]
</syntaxhighlight>


==JavaScript==
==JavaScript==
[[분류: JavaScript]]
[[분류: JavaScript]]
<source lang='JavaScript'>
<syntaxhighlight lang='JavaScript'>
console.log( "Hello World!".match(/\S+/g) );
console.log( "Hello World!".match(/\S+/g) );
// ["Hello", "World!"]
// ["Hello", "World!"]
console.log( "  This\tis    my\nC-14 Impaler gauss rifle!  ".match(/\S+/g) );
console.log( "  This\tis    my\nC-14 Impaler gauss rifle!  ".match(/\S+/g) );
// ["This", "is", "my", "C-14", "Impaler", "gauss", "rifle!"]
// ["This", "is", "my", "C-14", "Impaler", "gauss", "rifle!"]
</source>
</syntaxhighlight>


==PHP==
==PHP==
[[분류: PHP]]
[[분류: PHP]]
<source lang='PHP'>
<syntaxhighlight lang='PHP'>
$str = "Hello World!";
$str = "Hello World!";
print_r( array_filter(preg_split('/\s+/',$str)) );
print_r( array_filter(preg_split('/\s+/',$str)) );
68번째 줄: 72번째 줄:
#    [7] => rifle!
#    [7] => rifle!
# )
# )
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[함수 explode()]]
* [[함수 explode()]]
* [[화이트스페이스]]
* [[화이트스페이스]]

2020년 11월 2일 (월) 02:36 기준 최신판


1 C[ | ]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NUMBER_OF_WORDS 200

int main() {
    char *words[NUMBER_OF_WORDS];
    char seps[] = " \t\n";
    char str[] = "  This\tis    my\nC-14 Impaler gauss rifle!  ";

    int count = 0;
    char *token;
    token = strtok(str, seps);
    while( token != NULL ) {
        words[count] = malloc(strlen(token)+1);
        strcpy( words[count], token );
        count++;
        token = strtok(NULL, seps);
    }
    
    for(int i=0; i<count; i++) {
        printf("%d: [%s]\n", i, words[i]);
    }
    return 0;
}
// 0: [This]
// 1: [is]
// 2: [my]
// 3: [C-14]
// 4: [Impaler]
// 5: [gauss]
// 6: [rifle!]

2 JavaScript[ | ]

console.log( "Hello World!".match(/\S+/g) );
// ["Hello", "World!"]
console.log( "  This\tis    my\nC-14 Impaler gauss rifle!  ".match(/\S+/g) );
// ["This", "is", "my", "C-14", "Impaler", "gauss", "rifle!"]

3 PHP[ | ]

$str = "Hello World!";
print_r( array_filter(preg_split('/\s+/',$str)) );
# Array
# (
#     [0] => Hello
#     [1] => World!
# )

$str = "  This\tis    my\nC-14 Impaler gauss rifle!  ";
print_r( array_filter(preg_split('/\s+/',$str)) );
# Array
# (
#     [1] => This
#     [2] => is
#     [3] => my
#     [4] => C-14
#     [5] => Impaler
#     [6] => gauss
#     [7] => rifle!
# )

4 같이 보기[ | ]

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