Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim a string in C [duplicate]

Tags:

c

string

winapi

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

like image 530
Orion Edwards Avatar asked Mar 18 '09 00:03

Orion Edwards


People also ask

What does trim function do in C?

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.

How do you cut AC strings?

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.

Can you modify a string in C?

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).


1 Answers

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.

like image 50
Andrew Grant Avatar answered Sep 21 '22 12:09

Andrew Grant