Char * strchr (const char * STR, int CHR );
Search for the first CHR in Str. If no CHR is found, 0 is returned.
Char * strrchr (const char * STR, int CHR );
Search for the last CHR in Str. If no CHR is found, 0 is returned.
Char * strstr (const char * STR, const char * substr );
Search for the first occurrence of substr in Str. If no value is found, 0 is returned.
Size_t strspn (const char * STR, const char * chrset );
Search for the maximum number of characters at the beginning that contain only characters in the chrset.
Strspn ("cabbage", "ABC") = 5;
Strspn ("cabbage", "DC") = 1;
Strspn ("cabbage", "D") = 0;
Strspns (STR, "abceg") = 7;
Char * strspnp (const char * STR, const char * chrset );
Similar to strspns, it only returns pointers.
Const char * STR = "cabbage"
Strspnp (STR, "ABC") = STR + 5;
Strspnp (STR, "DC") = STR + 1;
Strspnp (STR, "D") = STR + 0;
Strspnp (STR, "abceg") = 0; // It is different from strspns.
Size_t strcspn (const char * STR, const char * chrset );
Search for the character location in the chrset for the first time in Str. If no character location is found, the '\ 0' position at the end of STR is returned.
Strcspn ("xyzbxz", "ABC") = 3
Strcspn ("xyzbxz", "XYZ") = 0
Strcspn ("xyzbxz", "No match") = 6
Strcspn ("xyzbxz", "") = 6
Strcspn ("", "ABC") = 0
Strcspn ("", "") = 0
Char * strpbrk (const char * STR, const char * chrset );
Similar to strcspns, it only returns pointers.
Strpbrk ("xyzbxz", "ABC") = "xyzbxz" + 3
Strpbrk ("xyzbxz", "XYZ") = "xyzbxz" + 0
Strpbrk ("xyzbxz", "No match") = 0 // is different from strcspns
Strpbrk ("xyzbxz", "") = 0 // not the same as strcspns
Strpbrk ("", "ABC") = 0 // different from strcspns
Strpbrk ("", "") = 0 // different from strcspns
------------------------
If you want to split "123 @ 456 @ 789" into "123" "456" "789", use strchr
If you want to split "123 @ #456 @ #789" into "123" "456" 789 ", use strstr
If you want to split "123 @ 456 #789 @ #0" into "123" "456" "789" 0 ", use strcspns
If you want to split "123 @ 456 #789 @ #0" into "123" "456" "789" "0 ",
The Code is as follows: (not tested)
# Include <stdio. h>
# Include <string. h>
# Include <assert. h>
Void split001 (const char * STR, const char CHR)
{
Const char * P1 = STR;
For (const char * P2; (P2 = strchr (P1, CHR ))! = 0; P1 = P2 + 1)
{
Printf ("\" %. * s \ "\ n", P2-P1, P1 );
}
Printf ("\" % s \ "\ n", P1 );
}
Void split002 (const char * STR, const char * substr)
{
Assert (* substr );
Const size_t substrlen = strlen (substr );
Const char * P1 = STR;
For (const char * P2; (P2 = strstr (P1, substr ))! = 0; P1 = P2 + substrlen)
{
Printf ("\" %. * s \ "\ n", P2-P1, P1 );
}
Printf ("\" % s \ "\ n", P1 );
}
Void split003 (const char * STR, const char * substr)
{
Assert (* substr );
Const char * P1 = STR;
For (const char * P2; P2 = p1 + strcspn (P1, substr), * P2; P1 = P2 + 1)
{
Printf ("\" %. * s \ "\ n", P2-P1, P1 );
}
Printf ("\" % s \ "\ n", P1 );
}
Void split004 (const char * STR, const char * substr)
{
Assert (* substr );
Const char * P1 = STR;
For (const char * P2; P2 = p1 + strcspn (P1, substr), * P2; P1 = P2 + strspn (P2, substr ))
{
Printf ("\" %. * s \ "\ n", P2-P1, P1 );
}
Printf ("\" % s \ "\ n", P1 );
}
Int main (void)
{
Split001 ("123 @ 456 @ 789 ",'@');
Printf ("--- \ n ");
Split002 ("123 @ #456 @ #789 ","@#");
Printf ("--- \ n ");
Split003 ("123 @ 456 #789 @ #0 ","@#");
Printf ("--- \ n ");
Split004 ("123 @ 456 #789 @ #0 ","@#");
Printf ("--- \ n ");
Return 0;
}