Strings(3)

πŸ“¦ version 1.0, in fedora 43, fedora 44

πŸ’– 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 as addslashes(), except this one only does the thing while n is greater than 0.
  • char * addslashes (char *s) β€” Add slashes to a string when necessary.
  • char * stripnslashes (char *s, int n) β€” Strip no more than n slashes 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 than n.
  • 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 than n chars.
  • 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 parse
  • n β€” 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 parse
  • token β€” 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 (just s is 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 replaced
  • with β€” String to replace with
  • n β€” 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 parse
  • delim β€” Character to search that will be replaced
  • with β€” 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 search
  • start β€” Initial offset to begin search
  • count β€” 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 parse
  • n β€” 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 done
  • ch β€” Character to search
  • count β€” 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 done
  • ch β€” Character to search
  • count β€” 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 from
  • start β€” Initial offset
  • count β€” 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