Different ways to access characters in a given String in C++
String class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character. There are several ways to access substrings and individual characters of a string. The string class supports the following functions for this purpose:
Let’s start discussing each of these methods in detail.
operator returns the reference to the character at the position specified as the argument.
char& operator(size_t pos);
pos is the index of the character to be searched.
Below is the C++ program to implement the operator function-
at() function is used for accessing individual characters. With this function, character by character can be accessed from the given string.
char& string::at (size_type idx)
Below is the C++ to implement at()-
substr() function is used for retrieving a substring from a given string. This function takes two values start and len.
- string.h is the header file required for string functions.
- The starting index is 0.
string substr (size_type start, size_type len);
start: Position of first character to be copied
len: Length of the sub-string.
Below is the C++ program to implement the substr() function-
find() function is used to find the first occurrence of a substring in the specified string being called upon.
- It returns the index of the first occurrence of the substring in the string.
- The default value of the starting position is 0.
- If the substring is not found then the function returns -1.
size_t find (const string& str, size_t pos = 0);
size_t find (const char* s, size_t pos = 0);
str is the substring to be searched.
s is the C-style substring to be searched.
pos is the initial position from where to start string search. The default value is 0.
Below is the C++ program to implement the find() function-
find_first_of() function is used to find the first character that matches any of the characters specified in the arguments. This function takes two arguments str and pos.
size_t find_first_of (const string& str, size_t pos = 0) const;
str is the string with characters to search for.
pos is the position of the first character in the string to be considered for search.
Below is the C++ program to implement find_first_of() function-
find_last_of() function is used for finding the location of last occurrence of the specified characters from the given string.
find_last_of(char ch, size_t position);
ch is the character to be searched in the string.
position is the index till where the search is to be performed.
Below is the C++ program to implement find_last_of() function-