-2

I have a string like this:

string str = "18:10"; 

18 is the minutes and 10 is the seconds.

I need to split the string str and store them into two int variables.

So essentially like this: int a = 18, int b =10. How do I do that?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
JY078
  • 393
  • 9
  • 21
  • [Check this one](http://stackoverflow.com/a/236803/6021126), then use std::stoi. – Andrii Bakal Mar 05 '16 at 05:47
  • 2
    Have you tried anything yourself? It seems like you just want community to do the job for you. – MarengoHue Mar 05 '16 at 06:22
  • Agreed with @MokonaModoki. YOu should try at least once. – Ashish Vora Mar 05 '16 at 06:31
  • I'm new with C++, I use python a lot, I just switched to C++. In python, I can just do str.split(":"). – JY078 Mar 05 '16 at 06:33
  • I don't see how this is a valid excuse. – MarengoHue Mar 05 '16 at 06:40
  • i'm so sorry,of course I tried myself. I tried: string str = "18:10"; istringstream iss(str); int n; while(iss>n) { int a; int b; cin >> a >>b;}. And it did not work. lol – JY078 Mar 05 '16 at 06:43
  • Does this answer your question? "[How do I iterate over the words of a string?](/q/236129/90527)", "[How can I split a string by a delimiter into an array?](/q/890164/90527)", "[How do I tokenize a string in C++?](/q/53849/90527)", "[Is There A Built-In Way to Split Strings In C++?](/q/599989/90527)", "[How can I convert a std::string to int?](/q/7663709/90527)", "[convert string to int use sstream](/q/18931990/90527)" – outis Jan 21 '23 at 20:30

3 Answers3

3

There's a few way to do this, C style (atoi, atof etc). In C++ we'd use std::stringstream from the header sstream.

#include <iostream>
#include <sstream>

template <typename T>
T convertString( std::string str ) {
    T ret;
    std::stringstream ss(str);
    ss >> ret;
    return ret;
}

int main() {
    std::string str = "18:10";
    int minutes,seconds;

    minutes = convertString<int>(str.substr(0,2));
    seconds = convertString<int>(str.substr(3,4));

    std::cout<<minutes<<" "<<seconds<<"\n";
}

Output:

18 10

This, of course, assumes that your string follow this format exactly (same number of integers, seperated by colon..). If you need to use this in a wider context, perhaps you'd be interested in using the std::regex utility instead.

058 094 041
  • 495
  • 3
  • 6
  • I wonder why you have the template? Could this work for other types as well such as `float` and `double`? – smttsp Mar 05 '16 at 05:56
  • @smttsp I believe so, looking at http://www.cplusplus.com/reference/istream/istream/operator%3E%3E/, _"Extracts and parses characters sequentially from the stream to interpret them as the representation of a value of the proper type, which is stored as the value of val...."_. Correct me if I'm wrong. – 058 094 041 Mar 05 '16 at 05:58
1

Try this code.

#include <string>
#include <sstream>

template <class NumberType, class CharType>
NumberType StringToNumber(const std::basic_string<CharType> & String)
{
    std::basic_istringstream<CharType> Stream(String);
    NumberType Number;
    Stream >> Number;
    return Number;
}

const std::string str("18:10");
const size_t Pos = str.find(':');
const auto Hour = StringToNumber<int>(str.substr(0, Pos));
const auto Minute = StringToNumber<int>(str.substr(Pos + 1, std::string::npos));

I didn't test it. Fix it if there is any error. You have to do error handling if your string may have empty parts for hours or minutes (e.g.; ":10", "18:" or ":").

hkBattousai
  • 10,583
  • 18
  • 76
  • 124
0
string str = "18:10";
string first_number, second_number;


int position =  str.find(':'); // remember thats it is counting from 0
for(int i = 0; i < position; i++){
   first_number = first_number + str[i];
}
cout << "first_number: " << first_number << endl;

for(int i = position+1; i < str.length(); i++){ // position+1 beacasue we dont wanna ':' in our string -> second_number
   second_number = second_number + str[i];
}
cout << "second_number: " << second_number << endl;
Nubet
  • 3
  • 2