We are currently in the process of converting the website to the new design. Some pages, like this one, are still broken. We appreciate your patience.
Handmade Network»Forums
117 posts
Code hacker/developer
Decent string lib for C?
Edited by Todd on
One of the things that C seems to be inherently weaker at is working with strings in a higher-level fashion. Python and similar languages have the .contains(), .split(), etc... functionality that C doesn't have by default and even if it's implemented, it tends to be more cumbersome to work with strings in C. Also the ability to do for s in my_strings { //do stuff} is very handy.

That all said, does anyone know of a good C string library or two? It's a bummer because I know C pretty well but I end up having to resort to other languages for small work projects just because it takes too long to get off the ground in C for these small utility projects that I often need to work on.

The downside to some of the other languages though is loss of power so I really would consider making some of these tools in C if I had access to better libraries that could help me get more done.
Mārtiņš Možeiko
2583 posts / 2 projects
Decent string lib for C?
Edited by Mārtiņš Možeiko on
From simpler, single/two file libraries:
Simple Dynamic Strings
Better String Library

A bit more header files, but more functionality:
µstr - Micro String API

For more universal strings (supports full unicode), but much more heavier:

13 posts
Decent string lib for C?
As a general purpose string library rapidstring looks promising. It emulates the ease of std::string for C while being a lot faster at it. But it doesn't have contains/split etc (doesn't need to).

As to C not having contains/split, that is not necessarily true, it is just that the standard library of C doesn't have the most user friendly interface. You should look into the standard header string.h and its use case.

"Find" is strstr.
"Find a single char" is strchr.
"Find any of" is strpbrk.
"Find first not of" is kindof strspn, like this
str + strspn(str, " \t\v\f\r\n")
It would skip all whitespace.

"Contains" is just checking
strstr(haystack, needle) != NULL

Split is strtok. Not the best api for splitting, but it works. You can implement splitting easily with strspn/strpbrk too.

What the C libraries don't have is a way to reverse search most of the time. Like a way to find the last occurence of X in a str. The reason probably is, that there is no straightforward interface for such a function, since you would have to pass the length of the string into the function.
Mikael Johansson
99 posts / 1 project
Decent string lib for C?
I have made my own string library, something I really recommend you to do.
Sure it takes a little while, but you will have it forever, and you just add the functionality you need for the current project. My string library follows me to every project I do. That way you will have a string library that does EXACTLY what you need it to.
56 posts / 1 project
Decent string lib for C?
Same here rolled my own never looked back. All to easy.