Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- char *= "name:454";
- #include <iostream>
- #include <sstream>
- #include <string>
- int main() {
- /* output storage */
- std::string id;
- double d;
- /* convert input to a STL string */
- std::string s("name:454");
- size_t off = std::string::npos;
- /* smart replace: parsing is easier with a space */
- if ((off = s.find(':')) != std::string::npos) { // error check: all or none
- s = s.replace(off, 1, 1, ' ');
- std::istringstream iss(s);
- iss >> id >> d;
- std::cout << "id = " << id << " ; d = " << d << 'n';
- }
- return 0;
- }
- #include <stdio.h>
- #include <string.h>
- int main ()
- {
- char str[] = "name:454";
- char* c = strtok(str, ":");
- while (c != NULL)
- {
- printf("%sn", c);
- c = strtok(NULL, ":");
- }
- return 0;
- }
- ^(w+):(d+)$
- template < typename T >
- T valueFromString( const std::string &src )
- {
- std::stringstream s( src );
- T result = T();
- s >> result;
- return result;
- }
- std::string wordByNumber( const std::string &src, size_t n, const std::string &delims )
- {
- std::string::size_type word_begin = 0;
- for ( size_t i = 0; i < n; ++i )
- {
- word_begin = src.find_first_of( delims, word_begin );
- }
- std::string::size_type word_end = src.find_first_of( delims, word_begin );
- word_begin = std::string::npos == word_begin || word_begin == src.length() ? 0 : word_begin + 1;
- word_end = std::string::npos == word_end ? src.length() : word_end;
- return src.substr( word_begin, word_end - word_begin);
- }
- char *a = "asdfsd:5.2";
- std::cout << wordByNumber( a, 0, ":" ) << ", " << valueFromString< double > ( wordByNumber( a, 1, ":" ) );
Add Comment
Please, Sign In to add comment