HP-UX Reference (11i v2 07/12) - 3 Library Functions N-Z (vol 7)

s
string(3C) string(3C)
unsigned char. Null pointer values for s1 and s2 are treated the same as pointers to
empty strings.
strncmp() makes the same comparison but examines a maximum of n
characters (n less than or equal to zero yields equality).
strcasecmp() and
strncasecmp() are identical in function to strcmp() and
strncmp() respectively,
but characters are folded by
_tolower() (see conv(3C)) prior to comparison. The
returned lexicographic difference reflects the folding to lowercase.
strcpy() Copies string s2 to s1, stopping after the null byte has been copied.
strncpy() copies
exactly n characters, truncating s2 or adding null bytes to s1 if necessary, until a total of n
have been written. The result is not null-terminated if the length of s2 is n or more. Each
function returns s1. Note that
strncpy()
should not be used to copy n bytes of an arbi-
trary structure. If that structure contains a null byte anywhere,
strncpy() copies
fewer than n bytes from the source to the destination and fills the remainder with null
bytes. Use the
memcpy() function (see memory(3C)) to copy arbitrary binary data.
strdup() Returns a pointer to a new string which is a duplicate of the string to which s1 points. The
space for the new string is obtained using the
malloc() function (see malloc(3C)).
strlen() Returns the number of characters in s, not including the terminating null byte.
strchr() (strrchr()) Returns a pointer to the first (last) occurrence of character c in string s,ora
null pointer if c does not occur in the string. The null byte terminating a string is con-
sidered to be part of the string.
index() (rindex()) is identical to strchr()
(strrchr()), and is provided solely for portability of BSD applications.
strpbrk() Returns a pointer to the first occurrence in string s1 of any character from string s2,ora
null pointer if no character from s2 exists in s1.
strspn() (strcspn()) Returns the length of the maximum initial segment of string s1, which con-
sists entirely of characters from (not from) string s2.
strstr() (strrstr()) Returns a pointer to the first (last) occurrence of string s2 in string s1,ora
NULL pointer if s2 does not occur in the string. If s2 points to a string of zero length,
strstr() (strrstr() ) returns s1.
strtok() Considers the string s1 to consist of a sequence of zero or more text tokens separated by
spans of one or more characters from the separator string s2. The first call (with a nonnull
pointer s1 specified) returns a pointer to the first character of the first token, and writes a
null byte into s1 immediately following the returned token. The function keeps track of its
position in the string s1 between separate calls, so that subsequent calls made with the first
argument a null pointer work through the string immediately following that token. In this
way subsequent calls work through the string s1 until no tokens remain. The separator
string s2 can be different from call to call. When no token remains in s1, a null pointer is
returned.
strtok_r() is identical to strtok(), except that it expects to be passed the address of a character
string pointer as the third argument. It will use this argument to keep track of the current
position in the string being searched. It returns a pointer to the current token in the string
or a NULL value if there are no more tokens.
strcoll() Returns an integer greater than, equal to, or less than zero, according to whether the string
pointed to by s1 is greater than, equal to, or less than the string pointed to by s2. The com-
parison is based on strings interpreted as appropriate to the program’s locale (see Locale
below). In the ‘‘C’’ locale strcoll() works like strcmp().
strxfrm() Transforms the string pointed to by s2 and places the resulting string into the array
pointed to by s1. The transformation is such that if the strcmp() function is applied to
two transformed strings, it returns a value greater than, equal to, or less than zero,
corresponding to the result of the strcoll() function applied to the same two original
strings. No more than n bytes are placed into the resulting string, including the terminat-
ing null character. If the transformed string fits in no more than n bytes, the length of the
resulting string is returned (not including the terminating null character). Otherwise the
return value is the number of bytes that the s1 string would occupy (not including the ter-
minating null character), and the contents of the array are indeterminate.
strcoll() has better performance with respect to strxfrm() in cases where a given string is com-
pared to other strings only a few times, or where the strings to be compared are long but a difference in the
strings that determines their relative ordering usually comes among the first few characters.
HP-UX 11i Version 2: December 2007 Update − 2 − Hewlett-Packard Company 459