Briefly:
I'm after the equivalent of .NET's String.Trim
in C using the win32 and standard C api (compiling with MSVC2008 so I have access to all the C++ stuff if needed, but I am just trying to trim a char*
).
Given that there is strchr
, strtok
, and all manner of other string functions, surely there should be a trim function, or one that can be repurposed...
Thanks
The trim() function removes leading, trailing, and/or redundant characters from a string. String from which the specified characters are removed. Specifies the specific leading, trailing, or redundant character to trim. If specified, then that character substitutes for the space.
To trim a string in C#, we can use String. Trim method that trims a string from both ends by removing white spaces or a specified character or characters from the start and the end of a string. Code example of string.
The only difference is that you cannot modify string literals, whereas you can modify arrays. Functions that take a C-style string will be just as happy to accept string literals unless they modify the string (in which case your program will crash).
There is no standard library function to do this, but it's not too hard to roll your own. There is an existing question on SO about doing this that was answered with source code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With