strchr, wcschr, _mbschr Find a character in a string.char *strchr( const char *string, int c );wchar_t *wcschr( const wchar_t *string, wint_t c );unsigned char *_mbschr( const unsigned char *string, unsigned int c );Routine Required Header Compatibility strchr <string.h> ANSI, Win 95, Win NT wcschr <string.h> or <wchar.h> ANSI, Win 95, Win NT _mbschr <mbstring.h> Win 95, Win NT For additional compatibility information, see Compatibility in the Introduction.LibrariesLIBC.LIB Single thread static library, retail version LIBCMT.LIB Multithread static library, retail version MSVCRT.LIB Import library for MSVCRT.DLL, retail version Return ValueEach of these functions returns a pointer to the first occurrence of c in string, or NULL if c is not found.ParametersstringNull-terminated source stringcCharacter to be locatedResThe strchr function finds the first occurrence of c in string, or it returns NULL if c is not found. The null-terminating character is included in the search.wcschr and _mbschr are wide-character and multibyte-character versions of strchr. The arguments and return value of wcschr are wide-character strings; those of _mbschr are multibyte-character strings. _mbschr recognizes multibyte-character sequences according to the multibyte code page currently in use. These three functions behave identically otherwise.
Example/* STRCHR.C: This program illustrates searching for a character * with strchr (search forward) or strrchr (search backward). */#include <string.h> #include <stdio.h>int ch = 'r';char string[] = "The quick brown dog jumps over the lazy fox"; char fmt1[] = " 1 2 3 4 5"; char fmt2[] = "12345678901234567890123456789012345678901234567890";void main( void ) { char *pdest; int result; printf( "String to be searched: \n\t\t%s\n", string ); printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 ); printf( "Search char:\t%c\n", ch ); /* Search forward. */ pdest = strchr( string, ch ); result = pdest - string + 1; if( pdest != NULL ) printf( "Result:\tfirst %c found at position %d\n\n", ch, result ); else printf( "Result:\t%c not found\n" ); /* Search backward. */ pdest = strrchr( string, ch ); result = pdest - string + 1; if( pdest != NULL ) printf( "Result:\tlast %c found at position %d\n\n", ch, result ); else printf( "Result:\t%c not found\n" ); } OutputString to be searched: The quick brown dog jumps over the lazy fox 1 2 3 4 5 12345678901234567890123456789012345678901234567890Search char: r Result: first r found at position 12Result: last r found at position 30
char *p = strchr(a, '?');
Find a character in a string.char *strchr( const char *string, int c );wchar_t *wcschr( const wchar_t *string, wint_t c );unsigned char *_mbschr( const unsigned char *string, unsigned int c );Routine Required Header Compatibility
strchr <string.h> ANSI, Win 95, Win NT
wcschr <string.h> or <wchar.h> ANSI, Win 95, Win NT
_mbschr <mbstring.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.LibrariesLIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return ValueEach of these functions returns a pointer to the first occurrence of c in string, or NULL if c is not found.ParametersstringNull-terminated source stringcCharacter to be locatedResThe strchr function finds the first occurrence of c in string, or it returns NULL if c is not found. The null-terminating character is included in the search.wcschr and _mbschr are wide-character and multibyte-character versions of strchr. The arguments and return value of wcschr are wide-character strings; those of _mbschr are multibyte-character strings. _mbschr recognizes multibyte-character sequences according to the multibyte code page currently in use. These three functions behave identically otherwise.
* with strchr (search forward) or strrchr (search backward).
*/#include <string.h>
#include <stdio.h>int ch = 'r';char string[] = "The quick brown dog jumps over the lazy fox";
char fmt1[] = " 1 2 3 4 5";
char fmt2[] = "12345678901234567890123456789012345678901234567890";void main( void )
{
char *pdest;
int result; printf( "String to be searched: \n\t\t%s\n", string );
printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
printf( "Search char:\t%c\n", ch ); /* Search forward. */
pdest = strchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tfirst %c found at position %d\n\n",
ch, result );
else
printf( "Result:\t%c not found\n" ); /* Search backward. */
pdest = strrchr( string, ch );
result = pdest - string + 1;
if( pdest != NULL )
printf( "Result:\tlast %c found at position %d\n\n", ch, result );
else
printf( "Result:\t%c not found\n" );
}
OutputString to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890Search char: r
Result: first r found at position 12Result: last r found at position 30
char *p1;
p1 = &a[0];
while(*p1 != '\0')
{
if(*p1 == '\?')
{
*p1 = '\0';
break;
}
p1++;
}
{
char a[] = "test.asp?s=1";
char *p1;
p1 = &a[0];
cout<<"before: "<<p1<<endl;
while(*p1 != '\0')
{
if(*p1 == '?')
{
*p1 = '\0';
break;
}
p1++;
}
cout<<"After: "<<a<<endl;
}
你要的是这个结果吗?
before: test.asp?s=1
After: test.asp
char *p1 = chrchr(a, '?')
if(p1)*p1 = '\0';
#include<string.h>void main()
{
char a[] = "test.asp?s=1";
char *p = strchr(a, '?'); printf("a=%08X\n",a);
printf("p=%08X\n",p);
printf("%c\n",*p);
printf("%s\n",p);
getch();
}在TC2.0下面的结果为:
a=0000FFBE
p=0000FFC6
?
?s=1没有什么问题啊。