Register»Forums»Decent string lib for C?
119 posts

Code hacker/developer

Decent string lib for C?
1 year, 11 months ago Edited by Todd on Oct. 12, 2018, 6:14 a.m.
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.

I'm the smartest man in Athens because I know that I know nothing. ~ Socrates
Mārtiņš Možeiko
2125 posts / 1 project
Decent string lib for C?
1 year, 11 months ago Edited by Mārtiņš Možeiko on Oct. 12, 2018, 8:37 a.m.
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?
1 year, 11 months ago
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
101 posts / 1 project
Decent string lib for C?
1 year, 11 months ago
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.

The biggest obstacle to great software is lack of motivation. Motivate each other!
Instead of reinventing the wheel, we should put chariot wheels on jet planes!
58 posts
Decent string lib for C?
1 year, 11 months ago
Same here rolled my own never looked back. All to easy.