Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This should be an easy one... merging strings in VC++? 2

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
OMG... I can't believe how much I have forgotten in the last few years...

How do you merge 2 strings together???

Or add a number to the end of a string...?

such as in VB you could say...
Code:
treeView1.SelectedNode = treeView1.Nodes.Add([b]"Test" & (treeView1.Nodes.Count + 1)[/b])

But this does not work in C++...
Code:
treeView1->SelectedNode = treeView1->Nodes->Add([b]"Test" + (treeView1->Nodes->Count + 1)[/b]);

BTW, I am using VC++ .Net 2005 (express)

I tried to cast it to a string, but it says int can not be cast to a string...?
String(treeView1->Nodes->Count + 1)
or
(String)(treeView1->Nodes->Count + 1)

I know I will probably have to get a big DUH!!! stamp on my forehead when I see the answer, but I will give a star anyways ;-)

Thanks in advance,
Josh

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Hi,

Here's some commented sample code :

Code:
#include <strstream>//strstream are string based stream
#include <iostream>//to write to the standard output

int main ()
{
  std::strstream str_stream;
  int integer = 50;
  str_stream << "Test" << integer;//in your app replace 'integer' by '(treeView1->Nodes->Count + 1)'

  std::cout << str_stream.str () << std::endl;
  // strstreal::str() gives what have been write to the 
  // stream (returns a std::string object)

  return 0;
}

--
Globos
 
If you want to add two strings (say of type char*), you can do something like in this example :
Code:
#include <iostream>
#include <string>
using namespace std;//avoid to have to qualify the namespace of the STL classes we use

int main ()
{
  string str = "part one";
  str = str + " - " + "part two";
  //in a concatenation expression like above, there must be at least
  //one object of class std::string (operator+ is defined in this class).

  cout << str << endl;

  return 0;
}



More infos :

- The STL(Standard Template Library) documentation page :

- Documentation pages on the various basic streams :


--
Globos
 
By the way, strstreams are deprecated... use a stringstream in <sstream> instead.

It looks like you are using a managed String, so I don't know what the equivalent is for those or whether it would work.
 
ok... lol... How do you convert a std::strstream to System::String for use w/ .Net components (Like a treeView)?

The rest of the errors are gone now, so I guess the rest worked...

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
I think that is what I needed...
String ^s = std::string("bla").c_str();

I'll try it when I get home...

Thanks, people ;-)

Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top