1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Replacing Characters in a C++ String

Discussion in 'Software Development' started by hitman_in_wi, Sep 20, 2004.

Thread Status:
Not open for further replies.
  1. hitman_in_wi

    hitman_in_wi Thread Starter

    Joined:
    Aug 2, 2004
    Messages:
    29
    Hey guys, Im writing a program that loads product names into a TLabel->Caption. The problem arises when I want to display an &. Since Caption is a String, it sees the & and puts an underscore underneath the next character. Therefore, instead of getting "Barnes & Nobles" I get "Barnes _Nobles". I cannot simply change the name in the datafile to "Barnes && Nobles", because other Containers I use display the & just fine.

    Therefore, the best solution I can think of is to, before loading the name into the Caption, replace all '&' in a string with '&&'. I would like it to work no matter how many '&' are in the product name.

    How exactly do I do this? Ive looked at some of the string::replace functions, but I cant figure it out.

    Thanks!
     
  2. coderitr

    coderitr

    Joined:
    Oct 12, 2003
    Messages:
    3,015
    Is this homework?
     
  3. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    Here are 2 general examples on how to replace a char in a string with something else. You will have to figure out how to use the idea for your sitution.

    STL example
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    string replace_amp(const string& s) {
        string build_it;
        for (size_t i = 0 ; i < s.size() ; i++) {
            if (s[i] == '&') {
                build_it += "&&";
            } else {
                build_it += s[i];
            }
        }
        return build_it;
    }
    
    int main() {
        const string example = "1 & 2 & 3 & 4 & 5 & 6";
        cout << replace_amp(example) << endl;
    }
    
    
    boost lib example

    Code:
    #include <iostream>
    #include <string>
    #include <boost/regex.hpp>
    
    using namespace std;
    using namespace boost;
    
    int main() {
        const string example = "1 & 2 & 3 & 4 & 5 & 6";
        cout << regex_replace(example,regex("&"),"&&") << endl;
    }
    
    
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/276047

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice