Advertisement
Vita94

C++ Tutorial 12 - Strings

Feb 17th, 2014
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  string s;
  8.  cout << "s = " << s << endl;
  9.  
  10.  s = "This is nice!";
  11.  cout << "s = " << s << endl;
  12.  
  13.  cout << s.at(1) << endl;
  14.  
  15.  string word;
  16.  cout << "Enter a word to append: "; cin >> word;
  17.  
  18.  s += word;
  19.  cout << "s = " << s << endl;
  20.  
  21.  cout << s.length() << endl;
  22.  
  23.  s.clear();
  24.  cout << "s = " << s << endl;
  25.  
  26.  s = "ABC";
  27.  cout << "s = " << s << endl;
  28.  
  29.  s.append("DEF");
  30.  cout << "s = " << s << endl;
  31.  
  32.  s.insert(3,"123"); //inserts string after 3 character
  33.  cout << "s = " << s << endl;
  34.  
  35.  s.replace(0,3,"XXX"); //replaces string starting from 0 to number of chars with sa XXX
  36.  cout << "s = " << s << endl;
  37.  
  38.  cout << s.find("123") << endl;
  39.  
  40.  cout << s.substr(3) << endl;
  41.  cout << s.substr(3,2) << endl;
  42.  
  43.  system("Pause");
  44.  return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement