1 C[ | ]
C
Copy
#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[ | ]
JavaScript
Copy
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[ | ]
PHP
Copy
$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 같이 보기[ | ]
편집자 Jmnote Jmnote bot
로그인하시면 댓글을 쓸 수 있습니다.