PS-Trainer C - Entwicklung
String-Funktionen: 1 String ReadOnly
Homepage von PS-Trainer - C-Entwicklung - Strings - an PS-Trainer
PS-Trainer PS-Trainer

Strings & characters Overview
strlen, wcslen, _mbslen, _mbstrlen Get the length of a string.
strstr, wcsstr, _mbsstr Find a substring.
strcspn, wcscspn, _mbscspn Find a substring in a string.
strspn, wcsspn, _mbsspn Find the first substring.
strtok, wcstok, _mbstok Find the next token in a string.


strlen, wcslen, _mbslen, _mbstrlen
Get the length of a string.
size_t strlen( const char *string );
size_t wcslen( const wchar_t *string );
size_t _mbslen( const unsigned char *string );
size_t _mbstrlen( const char *string );


Routine Required Header Compatibility
 strlen <string.h> ANSI, Win 95, Win NT
 wcslen <string.h> or <wchar.h> ANSI, Win 95, Win NT
 _mbslen <mbstring.h> Win 95, Win NT
 _mbstrlen <stdlib.h> Win 95, Win NT

Remarks
Each of these functions returns the number of characters in string, not including the terminating null character. wcslen is a wide-character version of strlen; the argument of wcslen is a wide-character string. wcslen and strlen behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcslen strlen _mbslen wcslen

_mbslen and _mbstrlen return the number of multibyte characters in a multibyte-character string.
_mbslen recognizes multibyte-character sequences according to the multibyte code page currently in use; it does not test for multibyte-character validity.
_mbstrlen tests for multibyte-character validity and recognizes multibyte-character sequences according to the LC_CTYPE category setting of the current locale. For more information about the LC_CTYPE category, see setlocale.

Subject String Manipulation Routines, Locale Routines
Keywords See also setlocale, strcat, strcmp, strcoll Functions, strcpy, strrchr, _strset, strspn
Return Value
Each of these functions returns the (integer) number of characters in string, excluding the terminal NULL.
No return value is reserved to indicate an error.

Parameter
string Null-terminated string

Example
/* STRLEN.C */

#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <dos.h>

void main( void )
{
char buffer[61] = "How long am I?";
int len;
len = strlen( buffer );
printf( "'%s' is %d characters long\n", buffer, len );
}


Output
'How long am I?' is 14 characters long


strstr, wcsstr, _mbsstr
Find a substring.
char *strstr( const char *string, const char *strCharSet );
wchar_t *wcsstr( const wchar_t *string, const wchar_t *strCharSet );
unsigned char *_mbsstr( const unsigned char *string, const unsigned char *strCharSet );


Routine Required Header Compatibility
 strstr <string.h> ANSI, Win 95, Win NT
 wcsstr <string.h> or <wchar.h> ANSI, Win 95, Win NT
 _mbsstr <mbstring.h> Win 95, Win NT

Remarks
The strstr function returns a pointer to the first occurrence of strCharSet in string. The search does not include terminating null characters.
wcsstr and _mbsstr are wide-character and multibyte-character versions of strstr. The arguments and return value of wcsstr are wide-character strings; those of _mbsstr are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcsstr strstr _mbsstr wcsstr

Subject: String Manipulation Routines
Keywords: See Also strcspn, strcmp, strpbrk, strrchr, strspn
Return Value
Each of these functions returns a pointer to the first occurrence of strCharSet in string, or NULL if strCharSet does not appear in string. If strCharSet points to a string of zero length, the function returns string.

Parameters
string Null-terminated string to search
strCharSet Null-terminated string to search for

Example
/* STRSTR.C */

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

char str[] = "lazy";
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%s\n", string );
printf( "\t%s\n\t%s\n\n", fmt1, fmt2 );
pdest = strstr( string, str );
result = pdest - string + 1;
if( pdest != NULL )
printf( "%s found at position %d\n\n", str, result );
else
printf( "%s not found\n", str );
}


Output
String to be searched:
The quick brown dog jumps over the lazy fox
1 2 3 4 5
12345678901234567890123456789012345678901234567890

lazy found at position 36


strcspn, wcscspn, _mbscspn
Find a substring in a string.
size_t strcspn( const char *string, const char *strCharSet );
size_t wcscspn( const wchar_t *string, const wchar_t *strCharSet );
size_t _mbscspn( const unsigned char *string, const unsigned char *strCharSet );


Routine Required Header Compatibility
 strcspn <string.h> ANSI, Win 95, Win NT
 wcscspn <string.h> or <wchar.h> ANSI, Win 95, Win NT
 _mbscspn <mbstring.h> Win 95, Win NT

Remarks
The strcspn function returns the index of the first occurrence of a character in string that belongs to the set of characters in strCharSet. Terminating null characters are included in the search.
wcscspn and _mbscspn are wide-character and multibyte-character versions of strcspn. The arguments of wcscspn are wide-character strings; those of _mbscspn are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcscspn strcspn _mbscspn wcscspn

Subject: String Manipulation Routines
Keywords: See Also strncat, strncmp, strncpy, _strnicmp, strrchr, strspn
Return Value
Each of these functions returns an integer value specifying the length of the initial segment of string that consists entirely of characters not in strCharSet. If string begins with a character that is in strCharSet, the function returns 0. No return value is reserved to indicate an error.

Parameters
string Null-terminated searched string
strCharSet Null-terminated character set

Example
/* STRCSPN.C */

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

void main( void )
{
char string[] = "xyzabc";
int pos;

pos = strcspn( string, "abc" );
printf( "First a, b or c in %s is at character %d\n",
string, pos );
}

Output
First a, b or c in xyzabc is at character 3


strspn, wcsspn, _mbsspn
Find the first substring.
size_t strspn( const char *string, const char *strCharSet );
size_t wcsspn( const wchar_t *string, const wchar_t *strCharSet );
size_t _mbsspn( const unsigned char *string, const unsigned char *strCharSet );


Routine Required Header Compatibility
 strspn <string.h> ANSI, Win 95, Win NT
 wcsspn <string.h> or <wchar.h> ANSI, Win 95, Win NT
 _mbsspn <mbstring.h> Win 95, Win NT

Remarks
The strspn function returns the index of the first character in string that does not belong to the set of characters in strCharSet. The search does not include terminating null characters.
wcsspn and _mbsspn are wide-character and multibyte-character versions of strspn. The arguments of wcsspn are wide-character strings; those of _mbsspn are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcsspn strspn _mbsspn wcsspn

Subject: String Manipulation Routines
Keywords: See also _mbsspnp, strcspn, strncat, strncmp, strncpy, _strnicmp, strrchr
Return Value
strspn, wcsspn, and _mbsspn return an integer value specifying the length of the substring in string that consists entirely of characters in strCharSet. If string begins with a character not in strCharSet, the function returns 0. No return value is reserved to indicate an error. For each of these routines, no return value is reserved to indicate an error.

Parameters
string Null-terminated string to search
strCharSet Null-terminated character set

Example
/* STRSPN.C: This program uses strspn to determine
* the length of the segment in the string "cabbage"
* consisting of a's, b's, and c's. In other words,
* it finds the first non-abc letter.
*/

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

void main( void )
{
char string[] = "cabbage";
int result;
result = strspn( string, "abc" );
printf( "The portion of '%s' containing only a, b, or c "
"is %d bytes long\n", string, result );
}


Output
The portion of 'cabbage' containing only a, b, or c is 5 bytes long


strtok, wcstok, _mbstok
Find the next token in a string.
char *strtok( char *strToken, const char *strDelimit );
wchar_t *wcstok( wchar_t *strToken, const wchar_t *strDelimit );
unsigned char *_mbstok( unsigned char*strToken, const unsigned char *strDelimit );


Routine Required Header Compatibility
 strtok <string.h> ANSI, Win 95, Win NT
 wcstok <string.h> or <wchar.h> ANSI, Win 95, Win NT
 _mbstok <mbstring.h> Win 95, Win NT

Remarks
The strtok function finds the next token in strToken. The set of characters in strDelimit specifies possible delimiters of the token to be found in strToken on the current call.
wcstok and _mbstok are wide-character and multibyte-character versions of strtok. The arguments and return value of wcstok are wide-character strings; those of _mbstok are multibyte-character strings. These three functions behave identically otherwise.

Generic-Text Routine Mappings
TCHAR.H _UNICODE & _MBCS _MBCS _UNICODE
Routine Not Defined Defined Defined
_tcstok strtok _mbstok wcstok

On the first call to strtok, the function skips leading delimiters and returns a pointer to the first token in strToken, terminating the token with a null character. More tokens can be broken out of the remainder of strToken by a series of calls to strtok. Each call to strtok modifies strToken by inserting a null character after the token returned by that call. To read the next token from strToken, call strtok with a NULL value for the strToken argument. The NULL strToken argument causes strtok to search for the next token in the modified strToken. The strDelimit argument can take any value from one call to the next so that the set of delimiters may vary.

Warning Each of these functions uses a static variable for parsing the string into tokens. If multiple or simultaneous calls are made to the same function, a high potential for data corruption and inaccurate results exists. Therefore, do not attempt to call the same function simultaneously for different strings and be aware of calling one of these function from within a loop where another routine may be called that uses the same function. However, calling this function simultaneously from multiple threads does not have undesirable effects.

Subject: String Manipulation Routines
Keywords: See Also strcspn, strspn, setlocale

Return Value
All of these functions return a pointer to the next token found in strToken. They return NULL when no more tokens are found. Each call modifies strToken by substituting a NULL character for each delimiter that is encountered.

Parameters
strToken String containing token(s)
strDelimit Set of delimiter characters

Example
/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/

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

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}


Output
A string of ,,tokens
and some more tokens

Tokens:
A
string
of
tokens
and
some
more
tokens


Homepage von PS-Trainer - C-Entwicklung - Strings - an PS-Trainer

Aktuelle Daten dieser Seite Letzte Änderung:
  Geocities

 

 

 

Hosted by www.Geocities.ws

1