-4

I am doing a Date class which takes a string as a parameter that is like :

11/13/2007  
9/23/2008  
9/23/2008

... and set it month, date and year objects, How can I use the substr() function, or are there any other functions ?

Quentin
  • 62,093
  • 7
  • 131
  • 191
pflove
  • 25
  • 8
  • 5
    Here is how you can do it: Go to Visual Studio, Create a new project (prefer Empty Project), `#include` your desired libraries and start coding. Keep searching and trying till it works. If you fail somewhere with a clear error come and post it here and we will be more than happy to help you. – Khalil Khalaf May 02 '16 at 13:33
  • 2
    Have you tried anything so far? There are countless example on this site and the internet that you can follow. case in point: http://stackoverflow.com/questions/236129/split-a-string-in-c – NathanOliver May 02 '16 at 13:34
  • I have already done this. But i cant do anything in this metod, i know how substr() works , it only takes where it should start and how many index it should go – pflove May 02 '16 at 13:36
  • You should be able to do this using [std::string::find](http://www.cplusplus.com/reference/string/string/find/) and [std::string::substr](http://www.cplusplus.com/reference/string/string/substr/). – Galik May 02 '16 at 13:36
  • "I have already done this." Done _what_? There is no code in sight. And if you're complaining that `substr` can't do what you want... good luck getting diagnostics when you haven't specified what you _do_ want. Flagging as unclear what you're asking. – underscore_d May 02 '16 at 13:37
  • When i use string a ; a.find("/") , it will return 2 index, so how can i use this? – pflove May 02 '16 at 13:38
  • Use this answer: http://stackoverflow.com/a/236803/5540644 Provides 2 functions, pass `split` the string you would like to split (i.e. `"11/13/2007 "`) and and the character you would like to split on (i.e. `'/'`). – Michelfrancis Bustillos May 02 '16 at 13:39
  • You can inspire you from this : https://github.com/plefebvre91/TokenizR – Pierre May 02 '16 at 13:44
  • Look at the instructions on how to use `std::string::substr` and see how the return value of `std::string::find` might help you. – Galik May 02 '16 at 13:47

2 Answers2

2

In your case, I would use the good old C(!) sscanf function:

unsigned int year, month, day;
char c;
if(sscanf(input, "%u/%u/%u%c", &month, &day, &year, &c) == 3)
{
    // check ranges of your date variables!
}

You seem to use American date format, so I set the parameters accordingly.

Wondering why I'm reading an additional character ? It catches additional data at the end of your date string to detect invalid formats (if data follows, result will be 4, otherwise, only 3 values are read, which will be returned). One drawback: before the three numbers, white space is ignored, so if you wanted to disallow, you would need additional checks (e. g. "%n %n%u" and comparing, if the two corresponding values are equal).

See sscanf documentation (scanf for parameters).

(If your input is a ::std::string instance, you need to use sscanf(input.c_str(), ...), of course.)

Farhad
  • 4,119
  • 8
  • 43
  • 66
Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • this is pretty good technique for this particular case. @pflove, try to use this. – Amit Tiwari May 02 '16 at 13:43
  • Just one addition: you get the date converted to numerical data already! I assumed this to be desired, as the question is tagged string-*parsing*. If not, see Michelfrancis' comment to the question. – Aconcagua May 02 '16 at 13:52
  • Pinpoint!This will help me so much, thanks! – pflove May 02 '16 at 13:59
1

If you are using C++11 you can use <regex> library:

#include <iostream>
#include <regex>
#include <string>

int main() {
    std::string date = "1/1/1970";
    std::regex dateRegex("(\\d\\d?)/(\\d\\d?)/(\\d{4})");
    std::smatch match;
    if (regex_match(date, match, dateRegex)) {
        std::cout << match.str(1) << std::endl;
        std::cout << match.str(2) << std::endl;
        std::cout << match.str(3) << std::endl;
    }
    else {
        // error
    }
    return 0;
}

This is maybe an overkill for your situation but remember that by using regular expression you are also doing some form of validation on your input. In above example, if you pass anything that doesn't have the form dd/dd/dddd where "d" is a digit you will enter the "error" block of code. Of course, 99/99/9999 will count as a valid input but you can also solve that case with more complicated regex.

Another option for parsing strings with delimiter is using getline function.

Josip Vujcic
  • 102
  • 1
  • 9