In C++ is there any way to extract a time from a string?
CString str;
str="17:18:58,9187120";
Is there any utility for converting this string in to a Time variable?
see this question:
Convert a string to a date in C++
The POCO library has a DateTimeParser class which might be helpful:
http://www.appinf.com/docs/poco/Poco.DateTimeParser.html
Not in C++ or its standard library. C++ barely handles its own complexity, it's too much to ask for anything that isn't common to all cultures, platforms, galaxies and vegetables.
For now you can do with Boost's string conversion functions.
Related
Is there any in-built function in C++ library that is able to make case insensitive comparision of two strings ? I am aware of simple approaches like using toupper/tolower,writing function ourselves.I want to know if there is anything in string.h library or other which is able to meet above objective.Here,
strcasecmp of C don't support strings,so not much of help in C++.It only works with char *.
Any help would be very thankful.
P.S. Boost libraries won't be of much help.
Thanks.
If you are willing to call strcasecmp, then you can call it in C++ too:
int cmp_result = strcasecmp(s1.c_str(), s2.c_str());
There is none. C++ does not provide any meaningful textual support beyond simply storing it. You will have to look to ICU.
I am in the process of developing a small game in DirectX 10 and C++ and I'm finding it hell with the various different types of strings that are required for the various different directx / win32 function calls.
Can anyone recommend which of the various strings are available are the best to use, ideally it would be one type of string that gives a good cast to the other types (LPCWSTR and LPCSTR mostly). Thus far I have been using std::string and std::wstring and doing the .c_str() function to get it in to the correct format.
I would like to get just 1 type of string that I use to pass in to all functions and then doing the cast inside the function.
Use std::wstring with c_str() exactly as you have been doing. I see no reason to use std::string on Windows, your code may as well always use the native UTF-16 encoding.
I would stick to std::wstring as well. If you really need to pass std::string somewhere, you can still convert it on the fly:
std::string s = "Hello, World";
std::wstring ws(s.begin(), s.end());
It works the other way around as well.
If you're using Native COM (the stuff of #import <type_library>), then _bstr_t. It natively typecasts to both LPCWSTR and LPCSTR, and it meshes nicely with COM's memory allocation model. No need for .c_str() calls.
How many types of string classes are there in visual c++. I got confused when i was going through the msdn center.
I found this type under the namespace system
http://msdn.microsoft.com/en-us/library/system.string(v=VS.71).aspx
and then in the headers section, i found the string header definitions. This seemed different from the above. One thing i noticed, this one comes under the STL.
(pls see the comment for the link, i cant post two links in the same post)
which one is normally used? I'm finding a hard time getting around with the different string classes
Thanks in advance :)
Different libraries come with different string types:
In plain old C you would use char*, the C++ standard library provides std::string which is widely used in C++ development. (string is defined as typedef basic_string<char> string;)
Microsoft created the MFC CString class which is (was?) used in MFC style programming, Qt has its QString which is used in Qt programs. What you're mentioning with System.String is a .net string class which can only be used in Managed code (with .net).
I'd suggest to stick with std::string (#include <string>) if you're new to C++. It's standard and platform independent.
String types in common use in Microsoft code are char*, wchar_t*, LPSTR, LPTSTR, LPWSTR, LPCSTR, LPCTSTR, LPCWSTR, BSTR, OLESTR, UNICODE_STRING, String, string, wstring, _bstr_t, CString
The last 5 are classes. You pick the one that gives you the least conversion headaches, depending on what API you need to use:
std::string and wstring, standard C++ library
System::String, the string type for managed code
_bstr_t, wrapper for a BSTR, used in COM automation
CString, string type for the ATL and MFC libraries.
You're likely to encounter additional string types when you work with other APIs.
I need to check whether my CString object in MFC ends with a specific string.
I know that boost::algorithm has many functions meant for string manipulation and that in the header boost/algorithm/string/predicate.hpp could it be used for that purpose.
I usually use this library with std::string. Do you know a convenient way to use this library also with CString?
I know that the library is generic that can be used also with other string libraries used as template arguments, but it is not clear (and whether is possible) to apply this feature to CString.
Can you help me with that in case it is possible?
According to Boost String Algorithms Library, "consult the design chapter to see precise specification of supported string types", which says amongst other things, "first requirement of string-type is that it must [be] accessible using Boost.Range", and note at the bottom the MFC/ATL implementation written by Shunsuke Sogame which should allow you to combine libraries.
Edit: Since you mention regex in the comments below, this is all you really need to do (assuming a unicode build):
CString inputString;
wcmatch matchGroups;
wregex yourRegex(L"^(.*)$"), regex::icase);
if (regex_search(static_cast<LPCWSTR>(inputString), matchGroups, yourRegex))
{
CString firstCapture = matchGroups[1].str().c_str();
}
Note how we reduce the different string types to raw pointers to pass them between libraries. Replace my contrived yourRegex with your requirements, including whether or not you ignore case or are explicit about anchors.
Why don't you save yourself the trouble and just use CStringT::Right?
I have been using std::string in my code. I was going to make a std::string and pass it by reference. However, someone suggested using a char * instead. Something about std::string is not reliable when porting code. Is that true? I have avoided using char * as I would need to do some memory management for it. Instead I find using the std::string much easier to use.
Basically I have a 10 digit output that I am storing in this string. Atm, I am not sure which would be better to use.
std::string is part of the C++ Standard, and has been since 1998. It is available in all the current C++ compilers. There really is no portability reason not to use it. If you have an API that needs to use a C-style string, you can use the std::string's c_str() member to get one from a string:
std::string s = "foo";
int n = strlen( s.c_str() );
In C++, almost every string should be std::string unless another library requires a cstring, in which case you should still be using an std::string and passing string.c_str(), unless you're using functions that work with buffers.
However, if you're writing a library and exporting functions, it's better to use const char* parameters rather than std::string parameters for portability.
Using a char * you are sure that you will not get portability issues among libraries.
If a library exports a function that uses an std::string, it might have problems communicating with another library that has been linked against a different version of the standard library.
I think that there is nothing to worry about unless you are going to provide some API to 3rd party.
Just use std::string
There's nothing unportable about std::string that isn't also an issue with char *. std::string actually uses a char * internally...
string is better. There is nothing unreliable about it on any platform. If you're worried about passing large classes, you can pass const references of your strings into functions. Makes coding faster and less bug prone.
In addition to the fact thata it's easier, std::string will probably be more efficient. Its small string optimization can keep the 10 digits in the std::string object itself, instead of putting them in another memory block off the heap.