When creating applications, you always need to manipulate strings. Eina provides a very useful API for manipulating C strings:
If you have a string, such as
“Rasterman:Bluebugs:Tasn:Illogict:billiob:Puppet_Master”, and you want to
print it in an easily readable format, you can use the eina_str_split()
function to split the string using a delimiter. The first parameter is the
string to split, the second determines where to split the string, and the
final parameter is the maximum number of strings to split the string into. If
you set a number less than 1, it splits the string as many times as possible.
The function returns a newly-allocated NULL
-terminated array of strings,
or NULL, if it fails to allocate the array. Always remember to free the memory
allocated by the eina_str_split()
function.
char *nicks = "Rasterman:Bluebugs:Tasn:Illogict:billiob:Puppet_Master"; char **result_arr; int i; // Splitting the string with ':' delimiter result_arr = eina_str_split(names, ":", 0); // Printing the result for (i = 0; result_arr[i]; i++) printf("Nick : %s\n", result_arr[i]); // Remember to free memory free(arr[0]); free(arr);
Use the eina_str_tolower()
and eina_str_toupper()
functions. They
change the case for all characters of the given string. These functions modify
the original strings.
char *str; // Initialize the string str = malloc(sizeof(char) * 4); strcpy(str, "bsd"); // Change the string to uppercase eina_str_toupper((char **)&str); printf("%s\n", str); // Change the string to lowercase eina_str_tolower(&str); printf("%s\n", str); // Free the allocated memory free(str);
Use the eina_str_join()
function. The fist parameter is the buffer to
store the result, the second is the size of the buffer, the third is the
separator between the 2 strings, and the 2 final parameters are the stings to
be joined.
char *part1 = "Elementary powered by"; char *part2 = "Enlightenment Foundation Libraries"; char *res; size_t size; // Calculate the string size + 1 for the delimiter size = strlen(part1) + strlen(part2) + 1 // Allocate memory for the result res = malloc(sizeof(char) * size); // Join the strings eina_str_join(res, size, ' ', part1, part2); printf("%s\n", res); // Free the allocated memory free(res):
use the
eina_str_has_prefix()
or eina_str_has_suffix()
function. You can also
check whether a string has a particular extension with the
eina_str_has_extension()
function.
These functions return EINA_TRUE
if the given string contains the
specified prefix, suffix, or extension, and EINA_FALSE
if it does not.
char *names = "Carsten;Cedric;Tom;Chidambar;Boris;Philippe" if (eina_str_has_prefix(names, "Carsten")) printf("String starts with 'Carsten'") if (eina_str_has_suffix(names, "Philippe")) printf("String ends with 'Philippe'") if (eina_str_has_extension(names, "philippe")) printf("String has extension 'philippe'") else printf("String does not have extension "philippe)