Solved

Help with generic function to remove remove user defined characters from a string

Posted on 2009-07-14
11
344 Views
Last Modified: 2013-12-14
Experts,

I am having trouble developing a function that passes in a string by reference, and  a character, and then deletes all the characters from the string.

Something like:

word1=STRING_REMOVE_CHAR(word2,")

so if word2="I" "DONT"" LIKE" "SPEECH" MARKS"

word1 = I DONT LIKE SPEECH MARKS

I hope you can help


0
Comment
Question by:simondopickup
  • 4
  • 4
  • 3
11 Comments
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
0
 

Author Comment

by:simondopickup
Comment Utility
Thanks, they have helped and I have written something that works using string::erase.

However, how do i pass in the '  character into a function? I would pass in an inverted comma using the ' " ' notation - but cant pass in the ' character. Whose name has escaped me... :S

0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
Single quote :)

Just escape it thus:  '\''
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> However, how do i pass in the '  character into a function?

You need to add \ before, e. g.

    replacechar(str, '\'', '/');  // the middle argument is  '  \  '  '  without the spaces

Same applies for " if it one of the characters in a string or char array

   string str="want to pass double quotes \"\" within a string";

The \ is the so-called escape character. It doesn't count but only indicates that the next char isn't a special char. Hence, to pass a backslash within a literal you need two backslashes \\

   string path = "C:\\temp";
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
I was too slow and too chatty ...
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:simondopickup
Comment Utility
Hmmm...another problem - but related to trhe question....

When i extract a string from my log - i am extracting the character string 'C'.

The aim of this thread was to create a function that would delete the first and second single quotes.

Unexpectedly, the whole string 'C' is occupying a single character space when extracted and thus I can not delete the first and second single quotations. Does anyone now why a simple string extraction using istringstream object would do this?

Sorry - will award points in a min :)
0
 

Author Comment

by:simondopickup
Comment Utility
This is the ifstream extraction...
runway_file>>word1;
if (word1=="DR")
{
      runway_file>>direction;
      STRING_REMOVE_CHAR(direction,'\'');
}
This is the line of the input file
 DR            'C'

I want the direction="C" - but the 'C' is occupying 1 character space...??
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>>>> - i am extracting the character string 'C'.
That isn't a string it is a single char.

  char str[] = ;

A character sequence or array of char often is called a string.

Removing single chars from a char array means to shift left from position after the char to remove including the terminating zero char.

    int len = strlen(str) ;
    for (int i = 0; i < len; ++i)
          str[i] = str[i+1];
    len--;   // correct length for further use

Note, a char array is 0-based. By the above we copied the terminating zero char which is at pos str[len].

The str now has the contents:  "that is within single quotes\'"

To remove the final ' you could do

    str[--len] = '\0';

what first decrements the len variable and then sets a terminating zero behind the 's' of quotes.

   
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 300 total points
Comment Utility
>>>> runway_file>>word1;
>>>> if (word1=="DR")

If that compiles you don't have a char array but a string class object probably std::string.

Then the most efficient method to erasing single chars is

    size_t len = word1.size();
    char * p = &word1[0];
    char * n = &word1[0];

    for (;*p;p++)
       if (*p != '\') *n++ = *p;
       else           --len;
    word1.resize(len);

The above loop checks each char for not being an apostrophe and if so put it at the current position. if it is an apostrophe the current psoition was not incremented. Finally, the string was resized to the new length.

A better readable but slowervariante for the above  is

    size_t pos = 0;
    while(std::string::npos != (pos = word1.find('\'', pos))) str.erase(pos, 1);





0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 200 total points
Comment Utility
>> A better readable but slowervariante for the above  is
remove() is more readable, no?

remove(s.begin(), s.end(), '\'');
http://www.cplusplus.com/reference/algorithm/remove/
0
 

Author Closing Comment

by:simondopickup
Comment Utility
Sorry - hard to split these points. itsmeandnobodyelse did contribute a couple of methods that I have added to my arsenal. Thank you both
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio Community 2015 updates failed 5 72
MAC and PC - Cheap video editing tools 4 105
C++ question 3 44
Dialogbox API leak? 18 58
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now