Like Tree1Likes
  • 1 Post By hk_mp5kpdw

C++ removing of all occurrence of some character from string

This is a discussion on C++ removing of all occurrence of some character from string within the C++ Programming forums, part of the General Programming Boards category; i am new to this forum. i am in the process of converting the code from SAS to C++. I ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    6

    C++ removing of all occurrence of some character from string

    i am new to this forum. i am in the process of converting the code from SAS to C++. I am a intermediate C++ developer, and SAS beginner. We have 'COMPRESS' function in SAS which "Returns a character string with specified characters removed from the original string". It seems like removing of all occurrence of some characters from String. How can i do this in C++? here is the sample SAS one line of code, i need to convert it into C++.

    if compress(str2) = compress(str1) then valid_rec='N';

    how can i write this in C++? I appreciate your help in this regard.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    How's your "google-foo?"

    This took almost no effort -> http://support.sas.com/publishing/pu...haps/59343.pdf
    Function:COMPRESS

    Purpose:To remove specified characters from a character value.

    Syntax:

    COMPRESS(character-value <,'compress-list'>)

    character-value is any SAS character expression.

    compress-list is an optional list of the characters you want to remove.
    If this argument is omitted, the default character to be removed is a blank. If
    you include a list of values to remove, only those characters will be
    removed. If a blank is not included in the list, blanks will not be removed.

    If a length has not been previously assigned, the length of the resulting
    variable will be the length of the argument.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    100
    Try using strtok(). In more recent C-style languages, you'll usually have a String.split() function, which without doing anything to the original string, will return an array of strings that were tokenized by pulling out the "unwanted" characters. strtok() is gonna be a lot more trouble to use than that for more than one reason. It damages the string it's tokenizing, has to be called repetitively, etc. But you can copy the original string, keep calling strtok() on it to build up an array (or list) manually, and then re-aggregate those tokens into the original string minus the characters. It you're trying to split on multi-character strings though, the process will be more complicated.

    Edit: If you go this route, read up on strtok() first.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by jsrig88
    Try using strtok().
    I wouldn't. strtok is intended for string tokenisation, not the removal of specified characters from a string. There are other algorithms more suited for this task, e.g., the use of certain std::string functions, or std::remove_if.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,802
    There are many ways to do this. Are you working with a std::string object? If so, one such method would be to use the erase member function along with the std::remove_if function from the <algorithm> header.
    Salem likes this.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a String - Removing the newline character
    By jeanermand in forum C Programming
    Replies: 8
    Last Post: 02-26-2012, 03:35 AM
  2. How to occurrence digits in string function.
    By so6ick in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2010, 11:21 PM
  3. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 01:59 PM
  4. Using fgets, removing last character
    By Stack Overflow in forum C Programming
    Replies: 31
    Last Post: 07-07-2004, 05:38 PM
  5. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 09:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21