Strings(3)
π NAME π
okay so bestie, this page is literally just Strings β that's it, that's the name π
No little dash-description after it, the original kept it mysterious. It's Strings(3), from the Library Functions Manual, and it's your whole girl-helpers toolbox for strings. π
β¨ Detailed Description β¨
General string manipulation and utilities functions. That's the whole vibe β a grab bag of little helpers for slicing, dicing, slashing, trimming and building strings. π¦
πΈ Functions πΈ
char * str_base64_encode (char *str)β Encodes a given string into its base64 form.char * str_base64_decode (char *str)β Decode a base64 encoded string.char * addnslashes (char *s, int n)β Same asaddslashes(), except this one only does the thing whilenis greater than 0.char * addslashes (char *s)β Add slashes to a string when necessary.char * stripnslashes (char *s, int n)β Strip no more thannslashes from a string.char * stripslashes (char *str)β Strip slashes from a string.void ltrim (char *str)β Strip left white spaces from a string.void rtrim (char *str)β Strip right white spaces from a string.void trim (char *str)β Strip both left and right white spaces from a string.char * substr (char *src, const int start, const int count)β Copy part of a string.char ** explode (char *src, const char *token, int *total)β Create an array from a string separated by some special char.char * str_nreplace (char *src, const char *delim, const char *with, int n)β Replace characters in a string, but not more thann.char * str_replace (char *str, const char *delim, const char *with)β Replace characters in a string.int strnpos (char *s, char *ch, unsigned int count)β Returns the position of a character in a string, but parses no more thannchars.int strpos (char *s, char *ch)β Returns the position of a character in a string.char * strdel (char *s, int start, int count)β Delete characters from a string.char * recvline (FILE *s)β Reads an entire line.char * make_string (char *s,...)β Makes a string.
π Function Documentation π
π char* addnslashes (char * s, int n)
Literally the same as addslashes(), but this one only does the action while n is greater than 0. No cap. π
Parameters:
sβ String to parsenβ Number of characters to work with.
See also:
addslashes()
char *name = 'My test string is called Β΄foobarΒ΄';
puts(name); // will display My test string is called 'foobar'
name = addnslashes(name, 31);
puts(name); // will display My test string is called Β΄foobar'
π char* addslashes (char * s)
Adds slashes to a string when necessary. It drops a \ in every quote ('), apostrophe (') or backslash (\). It's useful when you're working with databases, for example, because someone can try to insert these characters to try and hack the application... π stay safe out there bestie.
Parameters:
*sβ String to parse
Returns:
The new string, with slashes
See also:
stripslashes, addnslashes
char *name = 'My test string is called Β΄foobarΒ΄';
puts(name); // will display My test string is called 'foobar'
name = addslashes(name);
puts(name); // will display My test string is called Β΄foobarΒ΄
π char** explode (char * src, const char * token, int * total)
Create an array from a string separated by some special char. It divides the src string into pieces, each one delimited by token, and stores the total of pieces in total. π§
Parameters:
srcβ String to parsetokenβ Character delimiter to search.totalβ An integer variable passed as reference, which stores the total of items of the array
Returns:
The array, where each item is one separated by token
char **pieces;
char *name = 'This,is,a,string,of,test';
int total, i;
pieces = explode(name, ',', &total);
for (i = 0; i < total; i++)
printf('Piece %d: %s0, i, *(pieces+i));
π void ltrim (char * str)
Strip left white spaces from a string. π
Parameters:
strβ String to parse
Returns:
The new string, without left spaces
Author:
Original code was contribution by Erik Jansson
See also:
rtrim, trim
char *s = ' String with spaces ';
printf('_%s_0, s);
s = ltrim(s);
printf('_%s_0, s);
π char* make_string (char * s, ...)
Makes a string. Works like printf(), with the difference that it returns a string that is the concatenation of the values passed as parameter. it's giving printf but it gives you the string back β¨
Parameters:
*sβ Initial String and optionally formatation parameters (justsis allowed)
Returns:
The new String
char *sql = make_string('INSERT INTO myTable VALUES ('%s', '%s', '%s')', varValue1, varValue2, varValue3);
Todo:
String limits/error checking π (the original says so, not me!!)
π char* recvline (FILE * s)
Reads an entire line. It reads a line from the file specified by the file pointer passed as parameter. This function is intended to replace the non-portable GNU getline() function. π¦
Parameters:
sβ File pointer to the file to read from.
Returns:
String containing the line read or NULL if no more line are available
Author:
Robert Csok
π void rtrim (char * str)
Strip right white spaces from a string. π
Parameters:
strβ String to parse
Returns:
The new string, without left spaces
Author:
Original code was contribution by Erik Jansson
See also:
ltrim, trim
char *s = ' String with spaces ';
printf('_%s_0, s);
s = rtrim(s);
printf('_%s_0, s);
π char* str_base64_decode (char * str)
Decode a base64 encoded string. π
Parameters:
*strβ Encoded String to decode
Returns:
The decoded string
See also:
str_base64_encode
π char* str_base64_encode (char * str)
Encodes a given string to its base64 form. π
Parameters:
*strβ String to convert
Returns:
Base64 encoded String
See also:
str_base64_decode
π char* str_nreplace (char * src, const char * delim, const char * with, int n)
Replace characters in a string, but not more than n. It replaces all occurrences of *delim on *src with characters pointed by *with, stopping after n chars. β¨
Parameters:
*srcβ String to parse*delimβ Character to search that will be replacedwithβ String to replace withnβ Maximum number of chars to parse
Returns:
The new string
See also:
str_replace
char *linux = 'Linux C';
linux = str_nreplace(linux, 'C', 'Cool', strlen(linux));
puts(linux);
// -- OR --
char *name = 'rAfAel steil';
name = str_nreplace(name, 'A', 'a', 3);
puts(name);
π char* str_replace (char * str, const char * delim, const char * with)
Replace characters in a string. It replaces all occurrences of *delim on *src with characters pointed by *with.
Heads up bestie, the original is very honest about this one: the problem with the following code is that the function only searches for the first character of *delim, ignoring the rest. Other problem is speed related: note that the function always compares the length of *with to do the correct action. π
Parameters:
srcβ String to parsedelimβ Character to search that will be replacedwithβ String to replace with
Returns:
The new string
See also:
str_nreplace
char *linux = 'Linux C';
linux = str_replace(linux, 'C', 'Cool');
puts(linux);
// -- OR --
char *name = 'rAfAel steil';
name = str_replace(name, 'A', 'a');
puts(name);
π char* strdel (char * s, int start, int count)
Delete characters from a string. Deletes count characters of s, starting in start. βοΈ
Parameters:
sβ String to searchstartβ Initial offset to begin searchcountβ Number of characters to delete
Returns:
The new string
See also:
strndel()
*txt = 'Some text to test anything';
puts(txt);
txt = strdel(txt, 2, 8);
puts(txt);
π char* stripnslashes (char * s, int n)
Strip no more than n slashes from a string. It strips the backslash character (\) from a string, stopping after n chars. π§
Parameters:
sβ String to parsenβ Maximum number of chars to parse
Returns:
The new string, without slashes
See also:
addslashes, stripslashes
char *name = 'My another string is called \Β΄blablabla\Β΄';
puts(name); // will display My another string is called Β΄blablablaΒ΄
name = stripslashes(name, 33);
puts(name); // will display My another string is called 'blablablaΒ΄
π char* stripslashes (char * str)
Strip slashes from a string. It strips the backslash character (\) from a string. πΈ
Parameters:
sβ String to parse
Returns:
The new string, without slashes
See also:
addslashes, stripnslashes
char *name = 'My another string is called \Β΄blablabla\Β΄';
puts(name); // will display My another string is called Β΄blablablaΒ΄
name = stripslashes(name);
puts(name); // will display My another string is called 'blablabla'
π int strnpos (char * s, char * ch, unsigned int count)
Returns the position of a character in a string, but parses no more than n chars. π
Parameters:
sβ String where the search will be donechβ Character to searchcountβ Maximum number of chars to parse before exiting the function
See also:
strpos()
π int strpos (char * s, char * ch)
Returns the position of a character in a string. π
Parameters:
sβ String where the search will be donechβ Character to searchcountβ Maximum number of ch to search
See also:
strnpos()
π char* substr (char * src, const int start, const int count)
Copy part of a string. Copies count characters from src, starting from start. βοΈ
Parameters:
srcβ String to copy fromstartβ Initial offsetcountβ Number of chars to copy
Returns:
The new string
char *part, *str = 'Test one, test two';
part = substr(str, 1, 5);
puts(part); // -> est o
π void trim (char * str)
Strip both left and right white spaces from a string. π
Parameters:
strβ String to parse
Returns:
The new string, without left spaces
Author:
Original code was contribution by Erik Jansson
See also:
ltrim, trim
char *s = ' String with spaces ';
printf('_%s_0, s);
s = trim(s);
printf('_%s_0, s);
okay that's the whole page bestie, you're officially a string girlie now π β¨
This one's from LibCGI, dated 13 Mar 2003 β yes, 2003, she's vintage and she ate. π«Άπ
π¦ where this page comes from
- fedora 43: libcgi-devel 1.0-43.fc43 Β· LicenseRef-Callaway-LGPLv2+
- fedora 43: libcgi-devel 1.0-43.fc43 Β· LicenseRef-Callaway-LGPLv2+
- fedora 44: libcgi-devel 1.0-44.fc44 Β· LicenseRef-Callaway-LGPLv2+
- fedora 44: libcgi-devel 1.0-44.fc44 Β· LicenseRef-Callaway-LGPLv2+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms