Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

(C++) std::string

 

STL container for storing char. Or: 'the thing you use for storing words'.

 

To use them, the STL header file string.h must be #included.

 

#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 << std::endl;

}

 

Use std::string instead of an array of char [0][1][2][3].

 

std::string code snippets

 

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

 

0.              AnsiToStr, convert AnsiString to std::string

1.              Append two std::strings, Append

2.              Append, append two std::strings

3.              Ask user to input a double, AskUserForDouble

4.              Ask user to input a std::string, AskUserForString

5.              AskUserForDouble, ask user to input a double

6.              AskUserForString, ask user to input a std::string

7.              Check if std::string can be converted to double, IsDouble

8.              Check if std::string can be converted to integer, IsInt

9.              Convert AnsiString to std::string

10.         Convert double to std::string

11.         Convert integer to std::string

12.         Convert std::string to AnsiString

13.         Convert std::string to double

14.         Convert std::string to integer

15.         Convert std::string to lower case

16.         Convert std::string to upper case

17.         Convert std::string to WideString

18.         Convert WideString to std::string

19.         DoubleToStr, convert double to std::string

20.         Find std::string in std::string

21.         FindString, find std::string in std::string

22.         Get the extension of a filename

23.         Get the path of a filename, GetPath

24.         GetExtension, get the extension of a filename

25.         GetPath, get the path of a filename

26.         IntToStr, convert integer to std::string

27.         IsDouble, check if std::string can be converted to double

28.         IsInt, check if std::string can be converted to integer

29.         LoopReader, reading a container looped

30.         Reading a container looped, LoopReader

31.         Remove the extension of a filename

32.         Remove the path of a filename, RemovePath

33.         RemoveExtension, remove the extension of a filename

34.         RemovePath, remove the path of a filename

35.         Replace a substring by another in a certain std::string once, ReplaceOnce

36.         Replace all substrings by another in a certain std::string, ReplaceAll

37.         ReplaceAll, replace all substrings by another in a certain std::string

38.         ReplaceOnce, replace a substring by another in a certain std::string once

39.         Seperate a std::string into multiple std::strings, seperated by a seperator

40.         SeperateString, seperate a std::string into multiple std::strings, seperated by a seperator

41.         StrToAnsi, convert std::string to AnsiString

42.         StrToDouble, convert std::string to double

43.         StrToInt, convert std::string to integer

44.         StrToLower, convert std::string to lower case

45.         StrToUpper, convert std::string to upper case

46.         StrToWide, convert std::string to WideString

47.         Trim the leading and trailing spaces from a std::string

48.         Trim, trim the leading and trailing spaces from a std::string

49.         WideToStr, convert WideString to std::string

 

External links

*       Boost string algorithms

 

References

[0]         Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 Chapter 5.8.5: 'Use string rather then zero-terminated arrays of char'.

[1]         Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 77: 'Use vector and string instead of arrays.'

[2]         Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 13: 'Prefer vector and string to dynamically allocated arrays'

[3]         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*'

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.