strlib.h
strlib.h
interface takes care of memory
allocation, ensuring that there is sufficient space to hold
the result of each string operation.
strlib.h
interface are expected
to treat all strings as immutable and refrain from writing
into the character array.
Functions | |
Concatenates two strings by joining them end to end. | |
Returns the character at position i in the string s . | |
Returns a copy of the substring of s consisting of the characters between index positions p1 and p2 , inclusive. | |
Takes a single character and returns a one-character string consisting of that character. | |
Returns the length of the string s . | |
Copies the string s into dynamically allocated storage and returns the new string. | |
Returns true if the strings s1 and s2 are equal. | |
Returns true if the strings s1 and s2 are equal, ignoring differences in case. | |
Returns -1 if string s1 comes before s2 in lexicographic order, 0 if they are equal, and +1 if s1 comes after s2 . | |
Returns true if s1 starts with s2 . | |
Returns true if s1 ends with s2 . | |
Searches for the character ch beginning at position start in the string text and returns the first index at which it appears or -1 if no match is found. | |
Searches for the string str beginning at position start in the string text and returns the first index at which it appears or -1 if no match is found. | |
Returns the last index of ch in text , or -1 if the search value does not appear. | |
Returns the last index of str in text , or -1 if the search value does not appear. | |
Returns a new string with all alphabetic characters converted to lowercase. | |
Returns a new string with all alphabetic characters converted to uppercase. | |
Converts an integer into the corresponding string of digits. | |
Converts a string of digits into an integer. | |
Converts a floating-point number into the corresponding string form. | |
Converts a string representing a real number into its corresponding value. | |
Returns a new string after removing any whitespace characters from the beginning and end of the argument. | |
Returns a quoted string that can be read by the C parser. | |
Returns a string that appears correctly in HTML by changing HTML special characters to character entities. | |
Returns the length of a NULL -terminated string array. | |
Finds a string in a NULL -terminated string array and returns the first index at which the string appears, or -1 if it is not found. |
string concat(string s1, string s2);
concat("ABC", "DE")
returns the
string "ABCDE"
.
Usage:
s = concat(s1, s2);
char charAt(string s, int i);
i
in the
string s
. This function is included in the
library to make the type string
a true abstract
type in the sense that all of the necessary operations can be
invoked using functions. Calling charAt(s, i)
is like selecting s[i]
, except that
charAt
checks to see if i
is
within the range of legal index positions, which extend
from 0 to stringLength(s)
. Calling
charAt(s, stringLength(s))
Usage:ch = charAt(s, i);
string substring(string s, int p1, int p2);
s
consisting of the
characters between index positions p1
and p2
,
inclusive. The following special cases apply:
p1
is less than 0, it is assumed to be 0.
p2
is greater than or equal to the length of the
string, p2
is set to stringLength(s) - 1
.
p2 < p1
, substring
returns the
empty string.
Usage:
t = substring(s, p1, p2);
string charToString(char ch);
charToString
function is useful, for example, if you need to concatenate
a string and a character. Since concat
requires
two strings, you must first convert the character into a string.
Usage:
s = charToString(ch);
int stringLength(string s);
s
.
Usage:
len = stringLength(s);
string copyString(string s);
s
into dynamically allocated storage
and returns the new string. This function is not ordinarily required
when this package is used on its own but is often necessary when you
are working with more than one string package.
Usage:
newstr = copyString(s);
bool stringEqual(string s1, string s2);
true
if the strings s1
and s2
are equal. For the strings to be
considered equal, every character in one string must
precisely match the corresponding character in the other.
Uppercase and lowercase characters are considered to be
different.
Usage:
if (stringEqual(s1, s2)) ...
bool stringEqualIgnoreCase(string s1, string s2);
true
if the strings s1
and s2
are equal, ignoring differences in case.
Usage:
if (stringEqualIgnoreCase(s1, s2)) ...
int stringCompare(string s1, string s2);
s1
comes before s2
in lexicographic order, 0 if they are equal, and +1 if
s1
comes after s2
.
Usage:
if (stringCompare(s1, s2) < 0) ...
bool startsWith(string s1, string s2);
true
if s1
starts with s2
.
Usage:
if (startsWith(s1, s2)) ...
bool endsWith(string s1, string s2);
true
if s1
ends with s2
.
Usage:
if (endsWith(s1, s2)) ...
int findChar(char ch, string text, int start);
ch
beginning at position
start
in the string text
and returns
the first index at which it appears or -1 if no match is found.
Usage:
p = findChar(ch, text, start);
int findString(string str, string text, int start);
str
beginning at position
start
in the string text
and returns
the first index at which it appears or -1 if no match is found.
Usage:
p = findString(str, text, start);
int findLastChar(char ch, string text);
ch
in text
,
or -1 if the search value does not appear.
Usage:
p = findLastChar(ch, text);
int findLastString(string str, string text);
str
in text
,
or -1 if the search value does not appear.
Usage:
p = findLastString(str, text);
string toLowerCase(string s);
Usage:
s = toLowerCase(s);
string toUpperCase(string s);
Usage:
s = toUpperCase(s);
string integerToString(int n);
integerToString(123)
returns
"123"
as a string.
Usage:
s = integerToString(n);
int stringToInteger(string s);
stringToInteger
signals an error condition.
Usage:
n = stringToInteger(s);
string realToString(double d);
realToString(23.45)
returns "23.45". The conversion is the same as
that used for "%G"
format in printf
.
Usage:
string s = realToString(d);
double stringToReal(string s);
stringToReal
signals an error condition.
Usage:
d = stringToReal(s);
string trim(string str);
Usage:
trimmed = trim(str);
string quoteString(string str);
Usage:
quoted = quoteString(str);
string quoteHTML(string str);
Usage:
quoted = quoteHTML(str);
int stringArrayLength(string array[]);
NULL
-terminated string array.
Usage:
len = stringArrayLength(array);
int searchStringArray(string str, string array[]);
NULL
-terminated string array and
returns the first index at which the string appears, or -1 if it
is not found.
Usage:
index = searchStringArray(str, array);