There is no standard way of trimming a string
, but it’s possible to implement using the methods in the string
class:
- Left trim: Use
find_first_not_of()
to find the characters to erase - Right trim: Use
find_first_of()
to find the characters to erase - Trim: Do a right trim followed by a left trim (or vice-versa)
C String Split Function
C program to split string by space into words In this C program, we are going to learn how to split a given string by spaces and storing the substring in an array of strings. The split method in Python returns a list of strings after breaking the given string by the specified separator. // regexp is the delimiting regular expression; // limit is limit the number of splits to be made str. Split (regexp = ', limit = string.count(str)). C string containing the characters to match. Return Value A pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if none of the characters of str2 is found in str1 before the terminating null-character.
Use erase()
to actually erase the characters.
Here is my implementation. It defaults to trimming whitespace characters but you can specify any string of characters as the second argument.
Here’s a test program:
And here’s the valgrind output: