re. Replacing Characters in a string using .replace Help

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

re. Replacing Characters in a string using .replace Help

Post by DGENXP »

Hi

I would like to know how to replace a certain type of character in a stringw i looked over the API and found .replace(T,T) so i Assumed that the below code would work.

Code: Select all

          stringw str = ModelName[List].c_str() ;
          str += " - ";
          str +=  List;
          str.replace(L" ",L"_");
But this did not work I also tried str->replace(L" ",L"_"); but was completely wrong.

What i want the code to do is replace any spaces in str with an underscore "_" and that is it. but obviously this is not a simple process either as with many other things with irrlicht though i must admit this is alot easier to use then most other graphics frameworks its just these niggly things that bother me.

What exactly does .replace do and am i using it correctly if not could somebody please help by giving me the correct code and also explaining why it is wrong as i personally cannot see any thing wrong with the code that i am using

Cheers

DGENXP
DGENXP
Posts: 124
Joined: Tue Dec 27, 2005 2:49 am
Contact:

Post by DGENXP »

Hi All

Ok I Got it working my programing skills are a little rusty.

Code: Select all

     
          stringw str = ModelName[List].c_str() ;
          str += " - ";
          str +=  List;
          wchar_t old(L' ');
          wchar_t nwe(L'_');
          str.replace(old, nwe);
          ModelsInScene->addItem(str.c_str());
is this the optimal way to code this or is there a simpler way

Any Info please post it would be much appreciated

Cheers

DGENXP
NatX
Posts: 5
Joined: Thu Jan 04, 2007 5:31 am

use STL

Post by NatX »

Not sure what env your in but in VC++ you can use the following:

Code: Select all

// string_replace.cpp : main project file.

#include "stdafx.h"
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;

using namespace System;

int main(array<System::String ^> ^args)
{

	string s("aaaXaaaXXaaXXXaXXXXaaa");
	cout << s << endl;
	replace(s.begin(), s.end(), 'X', 'Y');
	cout << s << endl;

	Console::ReadKey();
                return 0;

}
Code from: http://www.codeguru.com/cpp/tic/tic0179.shtml
Even he, to whom most things that most people would think were pretty smart were pretty dumb, thought it was pretty smart.
Post Reply