Solved

CString vs std::string

Posted on 2001-09-05
19
6,965 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Template syntax for variable length arrays 9 78
Should CArray be used for a list of pointers in C++? 19 141
Header of docx file 17 142
draw a Christmas tree by using a nested loop? 26 102
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

738 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