Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
113 lines (97 sloc) 6.01 KB

(C++) std::string

std::string is an STL container for storing char. Or: 'the thing you use for storing words'.

The definition of std::string is in string.h.

#include <iostream>
#include <string>

int main()
{
  const std::string s1 = "Hello";
  const std::string s2 = "World";
  const std::string s3 = s1 + " " + s2;
  std::cout << s3 << '\'n;
}

Prefer using std::string over an array of char [1][2][3][4].

std::string example

#include <cassert>
#include <string>

int main()
{
  const std::string s = "ABCD";
  assert(s.find("xx")==std::string::npos);
  assert(s.find("BC")!=std::string::npos);
}

std::string code snippets

Note that among these are also the more general container code snippets.

  1. AnsiToStr, convert AnsiString to std::string
  2. Append two std::strings, Append
  3. Append, append two std::strings
  4. Ask user to input a double, AskUserForDouble
  5. Ask user to input a std::string, AskUserForString
  6. AskUserForDouble, ask user to input a double
  7. AskUserForString, ask user to input a std::string
  8. Check if std::string can be converted to double, IsDouble
  9. Check if std::string can be converted to integer, IsInt
  10. Convert AnsiString to std::string
  11. Convert double to std::string
  12. Convert integer to std::string
  13. Convert std::string to AnsiString
  14. Convert std::string to double
  15. Convert std::string to integer
  16. Convert std::string to lower case
  17. Convert std::string to upper case
  18. Convert std::string to WideString
  19. Convert WideString to std::string
  20. DoubleToStr, convert double to std::string
  21. Find std::string in std::string
  22. FindString, find std::string in std::string
  23. Get the extension of a filename
  24. Get the path of a filename, GetPath
  25. GetExtension, get the extension of a filename
  26. GetLongestString, find the length of the std::string with the most characters in a container
  27. GetPath, get the path of a filename
  28. GetShortestString, find the length of the std::string with the least characters in a container
  29. IntToStr, convert integer to std::string
  30. IsDouble, check if std::string can be converted to double
  31. IsInt, check if std::string can be converted to integer
  32. LoopReader, reading a container looped
  33. Reading a container looped, LoopReader
  34. Remove the extension of a filename
  35. Remove the path of a filename, RemovePath
  36. RemoveExtension, remove the extension of a filename
  37. RemovePath, remove the path of a filename
  38. Replace a substring by another in a certain std::string once, ReplaceOnce
  39. Replace all substrings by another in a certain std::string, ReplaceAll
  40. ReplaceAll, replace all substrings by another in a certain std::string
  41. ReplaceOnce, replace a substring by another in a certain std::string once
  42. ReverseString, reverse a std::string
  43. Reverse a std::string, ReverseString
  44. Seperate a std::string into multiple std::strings, seperated by a seperator
  45. SeperateString, seperate a std::string into multiple std::strings, seperated by a seperator
  46. SimplifyPath, simplify a path
  47. StrToAnsi, convert std::string to AnsiString
  48. StrToDouble, convert std::string to double
  49. StrToInt, convert std::string to integer
  50. StrToLower, convert std::string to lower case
  51. StrToUpper, convert std::string to upper case
  52. StrToWide, convert std::string to WideString
  53. SumStringLength, sum the lengths of std::strings irn a container
  54. Trim the leading and trailing spaces from a std::string
  55. Trim, trim the leading and trailing spaces from a std::string
  56. WideToStr, convert WideString to std::string

External links

References

  1. Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 5.8.5: 'Use string rather then zero-terminated arrays of char'.
  2. Herb Sutter, Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. 2005. ISBN: 0-32-111358-6. Chapter 77: 'Use vector and string instead of arrays'.
  3. Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 13: 'Prefer vector and string to dynamically allocated arrays'
  4. Jarrod Hollingworth, Bob Swart, Mark Cashman, Paul Gustavson. Sams C++ Builder 6 Developer's Guide. ISBN: 0-672-32480-6. Chapter 3.1: 'Use a string class instead of char*'