0

How to convert

std::string strdate = "2012-06-25 05:32:06.963";

To some thing like this

std::string strintdate = "20120625053206963" // basically i removed -, :, space and .

I think I should use strtok or string functions, but I am not able to do it, can any one please help me here with sampel code.

so that i convert it to unsigned __int64 by using

// crt_strtoui64.c
#include <stdio.h>

unsigned __int64 atoui64(const char *szUnsignedInt) {
   return _strtoui64(szUnsignedInt, NULL, 10);
}

int main() {
   unsigned __int64 u = atoui64("18446744073709551615");
   printf( "u = %I64u\n", u );
}
venkysmarty
  • 11,099
  • 25
  • 101
  • 184

6 Answers6

3
bool nondigit(char c) {
    return c < '0' || c > '9';
}

std::string strdate = "2012-06-25 05:32:06.963";
strdate.erase(
    std::remove_if(strdate.begin(), strdate.end(), nondigit),
    strdate.end()
);

std::istringstream ss(strdate);
unsigned __int64 result;
if (ss >> result) {
    // success
} else {
    // handle failure
}

Btw, your representation as a 64-bit int might be a bit fragile. Make sure that the date/time 2012-06-25 05:32:06 is input as 2012-06-25 05:32:06.000, otherwise the integer you get out of the end is smaller than expected (and hence might be confused for a date/time in the year 2AD).

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • @jrok: because it's locale-specific, and I can't remember whether `operator>>` is too. Also because `isdigit` doesn't accept negative input, so there's an extra cast needed and an extra explanation to go with it. There's the template version of `isdigit` in ``, though, which avoids the latter problem at the cost (for me, anyway) of having to look it up before using it. – Steve Jessop Jun 26 '12 at 10:11
  • An alternative would be just to remove the 5 characters that the questioner listed, not use any kind of test for a digit. That would mean there are more kinds of malformed input that fail at the end, which is a good thing or a bad thing according to whether you want to catch bad input, or just get on with it and blame whoever gave you the bad input :-) – Steve Jessop Jun 26 '12 at 10:15
2

If your compiler supports C++11 features:

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    std::string s("2012-06-25 05:32:06.963");
    s.erase(std::remove_if(s.begin(),
                           s.end(),
                           [](const char a_c) { return !isdigit(a_c); }),
            s.end());
    std::cout << s << "\n";
    return 0;
}
hmjd
  • 120,187
  • 20
  • 207
  • 252
0

use string replace to replace the unwanted chars with no chars http://www.cplusplus.com/reference/string/string/replace/

thommie
  • 438
  • 5
  • 22
0

Here you go:

bool not_digit (int c) { return !std::isdigit(c); }

std::string date="2012-06-25 05:32:06.963";
// construct a new string
std::string intdate(date.begin(), std::remove_if(date.begin(), date.end(), not_digit));
Nim
  • 33,299
  • 2
  • 62
  • 101
0

I wouldn't use strtok. Here's a fairly simple method that just uses std::string member functions:

std::string strdate = "2012-06-25 05:32:06.963";
size_t pos = strdate.find_first_not_of("1234567890");
while (pos != std::string::npos)
{
    size_t endpos = strdate.find_first_of("1234567890", pos);
    strdate.erase(pos, endpos - pos);
    pos = strdate.find_first_not_of("1234567890");
}

This is not a super efficient approach, but it will work.

A perhaps more efficient approach might use a stringstream...

std::string strdate = "2012-06-25 05:32:06.963";

std::stringstream out;

for (auto i = strdate.begin(); i != strdate.end(); i++)
    if (std::isdigit(*i)) out << *i;

strdate = out.str();

I make no promises about time or space complexity, but I suspect that using string::erase multiple times might involve a little more memory shuffling.

Rook
  • 5,734
  • 3
  • 34
  • 43
0
std::string strdate = "2012-06-25 05:32:06.963";
std::string result ="";
for(std::string::iterator itr = strdate.begin(); itr != strdate.end(); itr++)
{
    if(itr[0] >= '0' &&  itr[0] <= '9')
    {
        result.push_back(itr[0]);
    }
}
Thiem Nguyen
  • 6,345
  • 7
  • 30
  • 50
babun
  • 11
  • 3