LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-19-2017, 11:25 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
Best method to trim left/right a string in C?


Hello,

I am employing usually the following function:

Trim is really useful and important if you programme in C.
There are usually the need to trim configuration files, to let the programme explore the content of user settings.

Code:
char *trim(char *str)
{
    size_t len = 0;
    char *frontp = str;
    char *endp = NULL;

    if( str == NULL ) { return NULL; }
    if( str[0] == '\0' ) { return str; }

    len = strlen(str);
    endp = str + len;

    /* Move the front and back pointers to address the first non-whitespace
     * characters from each end.
     */
    while( isspace((unsigned char) *frontp) ) { ++frontp; }
    if( endp != frontp )
    {
        while( isspace((unsigned char) *(--endp)) && endp != frontp ) {}
    }

    if( str + len - 1 != endp )
            *(endp + 1) = '\0';
    else if( frontp != str &&  endp == frontp )
            *str = '\0';

    /* Shift the string so that it starts at str so that if it's dynamically
     * allocated, we can still free it on the returned pointer.  Note the reuse
     * of endp to mean the front of the string buffer now.
     */
    endp = str;
    if( frontp != str )
    {
            while( *frontp ) { *endp++ = *frontp++; }
            *endp = '\0';
    }


    return str;
}
I recently made again another one, which does similar job of trimming left and right.

You can share your experience with trim.
 
Old 05-20-2017, 10:22 PM   #2
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
I'm really still not sure exactly what you are trying to do here... get rid of everything before the first whitespace and after the last?
 
Old 05-21-2017, 04:01 AM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018Reputation: 5018
You might want to take look at strtok(3) if this is about parsing user options:
Code:
test@ws1:/tmp$ cat strtok.c
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
        static char string[] = { "\t\t    option1=100,option2=200    " };

        char *s = string;
        char *token = NULL;

        while (( token  = strtok(s, " \t,") )) {
                printf("\"%s\"\n", token);
                s = NULL ;  /* strtok() only wants the pointer on the first invocation */
        }

        return 0;
}
test@ws1:/tmp$ cc -Wall -o strtok strtok.c 
test@ws1:/tmp$ ./strtok 
"option1=100"
"option2=200"
test@ws1:/tmp$
You would have to add additional code to cope with any embedded spaces in quoted strings such as a option3="a string with whitespace, and a comma.", and I get the feeling that could get quite torturous, but if they're not a consideration then it'll do the job of removing leading/trailing whitespace.

Last edited by GazL; 05-21-2017 at 04:21 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using SED with EXPECT to trim a string from buffer h4rri Linux - Newbie 11 11-10-2013 07:59 AM
[SOLVED] Most reliable method to get IP address (one string) ? patrick295767 Programming 3 10-25-2013 10:53 AM
[SOLVED] extract string from right to left socalheel Programming 3 09-05-2013 02:35 PM
The method contains(String) is undefined for the type String sampada Programming 1 06-30-2007 10:02 AM
syntax error in string trim and wrong node routing agent in tcl script newbie06 Linux - General 0 02-23-2007 02:00 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration