Solved

CString vs std::string

Posted on 2001-09-05
19
6,949 Views
Last Modified: 2012-08-13
hi,

I am working on this project..

I thought I had the back part done (read from file into list etc...a little string manipulation)


but, now I am having to make this work with MFC

I have lots of std::string and I need to be able to translate them to CStrings and maybe even sometimes into char*

can someone give me some ideas for this, or should I not be needing to do this?

in any case, the problem is that CString doesn't seem to have a substr or a find_first_of function, both of which I was using...

I know I can do it using char[], but that is supposed to be NOT the way to do it, I was using std::strings at the reccomendation of some of you all here, and know I'm stuck.



Thanks, if you need more details, let me know.
0
Comment
Question by:bebonham
  • 5
  • 5
  • 4
  • +3
19 Comments
 
LVL 6

Accepted Solution

by:
Triskelion earned 50 total points
ID: 6457881
You should be able to use either.
CString has a Find() method and Left, Mid, Right.
It should do everything you want.
0
 
LVL 2

Expert Comment

by:bkdc
ID: 6457885
err...why do you need CStrings ?
I mean, most of the Windows API uses char* and even MFC functions (most of them) support this method.
 As you said, std:string offers you all the features you need so I can't see any reason not to use it.
 Maybe I'm a bit rusty (3-4 months since my last experience with MFC) but as far as I remember CString does have substr and find
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6457898
...by the way
CString also has
FindReverse(),GetAt(),SetAt(),MakeUpper(),MakeLower(),MakeReverse(),TrimRight(),TrimLeft()

It can act as a "char *" or LPCTSTR or LPTSTR or BSTR
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 8

Author Comment

by:bebonham
ID: 6457926
thanks to you both, I am interested in what both of you have to say, so please explain, and I will split these 100 points between you, provided this all works okay.

Triskelion:

"It can act as a "char *" or LPCTSTR or LPTSTR or BSTR "

how do I make it act like this?

both as an L-Val and R-Val
_____

bkdc:

unfortunately, the other side of this is all MFC, so I have to compare CStrings to my std::strings, and pass CStrings back to this OLE thing so it updates my database.

just for example, how do I make AfxMessageBox take a std::string.?


Thanks to both of you
0
 
LVL 32

Expert Comment

by:jhance
ID: 6457962
>>just for example, how do I make AfxMessageBox take a std::string.?

Is you have a std::string called sMyString you could do:

AfxMessageBox(sMyString.c_str(), MB_OK);
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6457986
GetBuffer(0); // char *
GetBuffer({greater than 0}); //LPTSTR
GetAt() // TCHAR
AllocSysString(); // BSTR


... and of course, good-old casting.

The constructors also do some conversion for you
// Constructors
     CString();
     CString(const CString& stringSrc);
     CString(TCHAR ch, int nRepeat = 1);
     CString(LPCSTR lpsz);
     CString(LPCWSTR lpsz);
     CString(LPCTSTR lpch, int nLength);
     CString(const unsigned char* psz);
0
 
LVL 8

Author Comment

by:bebonham
ID: 6458086
thank you jhance, that is very helpfull...


Triskelion:

I am not too sure how to do this:

I have tried
std::string stds="the string in question";

cstr=static_cast<CString>(stds);
that didn't work

=========

CString cstr(LPCSTR stds);
AfxMessageBox(cstr);

and it works to make some kind of CString, but AfxMessageBox wont take it...

in short, I am not having success with your solution, because I didn't quite get it.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6458154
Some examples:

CString szCString1 = "Hello World";
std::string sz_string1 = szCString1.operator LPCTSTR();
std::string sz_string2 = (LPCTSTR)szCString1;
CString szCString2 = sz_string1.c_str();
0
 
LVL 30

Expert Comment

by:Axter
ID: 6458167
continue......

void somefunction(const char* data)
{
AfxMessageBox(data);
}

int main(int argc, char* argv[])
{
CString szCString1 = "Hello World";
std::string sz_string1 = szCString1.operator LPCTSTR();

somefunction(szCString1.operator LPCTSTR());
somefunction(sz_string1.c_str());
return 0;
}
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6458174
    std::string stds="the string in question";
     CString     strMyThang(stds.data());
     AfxMessageBox(stds.data());
     AfxMessageBox(strMyThang);
0
 
LVL 32

Expert Comment

by:jhance
ID: 6458194
I wonder if any of these suggestions could be more complicated.  If you want a std::string in a CString how about:

CString mStr(mystdstring.c_str());

Since CString has a constructor that takes a C string, this works nicely and CString and the c_str() member can handle all the dirty details.
0
 
LVL 8

Author Comment

by:bebonham
ID: 6458216
thanks to everyone!!

I am going to close this q now and post a request for split points in cs.

I would like to split them btwn Triskelion and Axter, because their comments have helped me the most.

I also want to thank jhance, because that bit of info about .c_str() was helpful!


Bob
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6458217
Yes, c_str() works just the same as (data())in my example

     std::string stds="the string in question";
     CString     strMyThang(stds.c_str());
     AfxMessageBox(stds.c_str());
     AfxMessageBox(strMyThang);
0
 
LVL 8

Author Comment

by:bebonham
ID: 6458225
0
 
LVL 30

Expert Comment

by:Axter
ID: 6458260
>>Yes, c_str() works just the same as (data())in my example

I've never seen the data() member function used in any code, but it does seem to be part of the ansi C++ standards, so I guess there's nothing wrong with using it.
However, using the c_str() member function is the norm for most code, and the c_str() member function is more descriptive then data().
0
 
LVL 32

Expert Comment

by:jhance
ID: 6458283
Is data() safe to use in this context?  The manual page for it doesn't explicitly say that it produces a NULL terminated string.  So unless the STL spec actually requires the string template to represent the data as a NULL terminated string, you could be making an assumption that the current implementation will always be the case.

Note that the c_str() manual page (at least from VC++) explicitly says:

"The member function returns a pointer to a nonmodifiable C string constructed by adding a terminating null element (E(0)) to the controlled sequence."

I think using data() is dangerous since you cannot be sure that the referenced data will always be NULL terminated.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6458306
jhance,
Good info.  
I was woundering why I never saw the function used before.
0
 
LVL 30

Expert Comment

by:Axter
ID: 6458440
FYI,
See following link:
http://www.dinkum.com/htm_cpl/string2.html#basic_string::data

and

http://www.dinkum.com/htm_cpl/string2.html#basic_string::c_str

On the data() link, it states that it is a non-null pointer.
Where as in the c_str link, returns a terminating null pointer.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6458652
Points split.  Axter here please:
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?qid=20179073

Thanks,
Moondancer
Community Support Moderator @ Experts Exchange
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

773 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