There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Software Development
Tag Cloud
audio blue screen boot bsod computer cpu crash dell desktop driver drivers error excel external hard drive firefox freezes freezing hard drive hardware hijackthis internet internet explorer ipod itunes laptop malware motherboard mouse network networking outlook 2007 power printer problem ram router screen slow sound trojan upgrade usb virtumonde virus vista vista 32-bit windows windows xp winxp wireless
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Replacing Characters in a C++ String


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. Enjoy!

Closed Thread
 
Thread Tools
hitman_in_wi's Avatar
Junior Member with 29 posts.
 
Join Date: Aug 2004
Experience: Intermediate
20-Sep-2004, 10:58 AM #1
Replacing Characters in a C++ String
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!
coderitr's Avatar
Distinguished Member with 3,080 posts.
 
Join Date: Oct 2003
20-Sep-2004, 10:31 PM #2
Is this homework?
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
21-Sep-2004, 03:21 AM #3
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;
}
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 12:08 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.