Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove Spaces (or other characters) from a String
#6
(12-29-2022, 01:28 AM)George McGinn Wrote: Awhile back I needed to remove SPACES (and sometimes other characters) from a string in some of my C code.

The need to do so in QB64 reared its ugly head again today, so I resurrected my code and now have incorporated it into any QB64 program I will need it in.

You pass the function 2 parameters - The string you want to change, and the character(s) you want removed. For example, if I want spaces and periods to be removed from a sentence, I pass " ." in the second argument. You don't need to run this function multiple times to accomplish this.

Here is the C/C++ Header file:
Code: (Select All)
#include <stdio.h>
/* search for character(s) */
int string_search_chr(char *tokens,char s){
        if (!tokens || s=='\0')
        return 0;
    for (;*tokens; tokens++)
        if (*tokens == s)
            return 1;
    return 0;
}
char *string_remove_chr(char *str,const char *tokens) {
    char *src = str , *dst = str;
    /* validate input */
    if (!(str && tokens))
        return NULL;
    while(*src)
        if(string_search_chr(tokens,*src))
            src++;
        else
            *dst++ = *src++;  /* assign first, then incement */
    *dst='\0';
    return str;
}

Here is the QB64 program that tests the above:
Code: (Select All)
DECLARE LIBRARY "./removeStr"
    FUNCTION __REMOVESPACES$ ALIAS string_remove_chr(qString$, charValue$)
END DECLARE

QBMain:
    PRINT "QB64: Test #1 - Remove SPACES from 'New York Yankees"
    qString$ = "New York Yankees"
    retValue$ = __REMOVESPACES$(qString$+CHR$(0), " ")
    PRINT "QB64: qString$ = "; qString$
    PRINT "QB64: retValue$ = "; retValue$
    PRINT
    PRINT "QB64: Used in a PRINT stmt, function returns: "; __REMOVESPACES$(qString$+CHR$(0), " ")  
    PRINT

    PRINT "QB64: Test #2 -Remove SPACES and PERIODS from 'New York Yankees."
    qString$ = "New York Yankees"
    retValue$ = __REMOVESPACES$(qString$+CHR$(0), " .")
    PRINT "QB64: qString$ = "; qString$
    PRINT "QB64: retValue$ = "; retValue$
    PRINT
    PRINT "QB64: Used in a PRINT stmt, function returns: "; __REMOVESPACES$(qString$+CHR$(0), " ")  
    PRINT

    SYSTEM 0

This is the output from running the above code:
Quote:QB64: Test #1 - Remove SPACES from 'New York Yankees
QB64: qString$ = New York Yankees
QB64: retValue$ = NewYorkYankees

QB64: Used in a PRINT stmt, function returns: NewYorkYankees

QB64: Test #2 -Remove SPACES and PERIODS from 'New York Yankees.
QB64: qString$ = New York Yankees
QB64: retValue$ = NewYorkYankees

QB64: Used in a PRINT stmt, function returns: NewYorkYankees



------------------
(program exited with code: 0)
Press return to continue

There's a couple of small glitches in this, that you might want to take a look at and fix, @George McGinn.

First, there's the whole need to set the compiler to allow -fpermissive so this will run.  Types don't match properly, so you might want to tweak them a little.

Second is this alters the original string, corrupting it, as shown below:
Code: (Select All)
Declare CustomType Library "removeStr"
    Function __REMOVESPACES$ Alias string_remove_chr (qString$, charValue$)
End Declare

check$ = "This is a string that we're going to check to see how long it would take to remove the spaces from."
Print check$
Print "The length of our test string is:"; Len(check$)
new$ = __REMOVESPACES$(check$, " ")
Print check$, Len(check$)
End


check$ remains the same length as previously, but the left side of it now mirrors new$.  You might want to assign your string to a temp string and process it, rather than process the original as you go along, to preserve it.  Wink
Reply


Messages In This Thread
RE: Remove Spaces (or other characters) from a String - by SMcNeill - 12-29-2022, 08:30 AM



Users browsing this thread: 5 Guest(s)