Guest User

Untitled

a guest
Jun 23rd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. char *= "name:454";
  2.  
  3. #include <iostream>
  4. #include <sstream>
  5. #include <string>
  6.  
  7. int main() {
  8. /* output storage */
  9. std::string id;
  10. double d;
  11. /* convert input to a STL string */
  12. std::string s("name:454");
  13. size_t off = std::string::npos;
  14. /* smart replace: parsing is easier with a space */
  15. if ((off = s.find(':')) != std::string::npos) { // error check: all or none
  16. s = s.replace(off, 1, 1, ' ');
  17. std::istringstream iss(s);
  18. iss >> id >> d;
  19. std::cout << "id = " << id << " ; d = " << d << 'n';
  20. }
  21. return 0;
  22. }
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26.  
  27. int main ()
  28. {
  29. char str[] = "name:454";
  30. char* c = strtok(str, ":");
  31.  
  32. while (c != NULL)
  33. {
  34. printf("%sn", c);
  35. c = strtok(NULL, ":");
  36. }
  37.  
  38. return 0;
  39. }
  40.  
  41. ^(w+):(d+)$
  42.  
  43. template < typename T >
  44. T valueFromString( const std::string &src )
  45. {
  46. std::stringstream s( src );
  47. T result = T();
  48. s >> result;
  49. return result;
  50. }
  51.  
  52. std::string wordByNumber( const std::string &src, size_t n, const std::string &delims )
  53. {
  54. std::string::size_type word_begin = 0;
  55. for ( size_t i = 0; i < n; ++i )
  56. {
  57. word_begin = src.find_first_of( delims, word_begin );
  58. }
  59. std::string::size_type word_end = src.find_first_of( delims, word_begin );
  60.  
  61. word_begin = std::string::npos == word_begin || word_begin == src.length() ? 0 : word_begin + 1;
  62. word_end = std::string::npos == word_end ? src.length() : word_end;
  63. return src.substr( word_begin, word_end - word_begin);
  64. }
  65.  
  66.  
  67. char *a = "asdfsd:5.2";
  68. std::cout << wordByNumber( a, 0, ":" ) << ", " << valueFromString< double > ( wordByNumber( a, 1, ":" ) );
Add Comment
Please, Sign In to add comment