# Strings Functions

## [Online Compiler](https://cpp.sh/)

<details>

<summary><a href="https://cplusplus.com/reference/string/string/substr/">string :: substr</a></summary>

```cpp
// string::substr
#include <iostream>
#include <string>
using namespace std;

int main (){
    string str="We think in generalities, but we live in details.";

    string str2 = str.substr (3,5);     // "think"

    size_t pos = str.find("live");      // position of "live" in str

    string str3 = str.substr (pos);     // get from "live" to the end

    cout << str2 << ' ' << str3 << '\n';

    return 0;
}
```

</details>

<details>

<summary><a href="https://cplusplus.com/reference/string/string/find/">string :: <strong>find()</strong></a>                                                                                                                                                               </summary>

{% code overflow="wrap" %}
```cpp
bool findStr(string haystack, string needle) {
    
    size_t found = haystack.find(needle);
   
    //The position of the first character of the first match.
    //If no matches were found, the function returns string::npos.
   
    if(found != string::npos)
       return true;
    else
       return false;
}

int main(){
    string haystack = "sabutsad", needle = "sadl";
    
    if(findStr(haystack, needle))
       cout << "String found!!";
    else
       cout << "String not found!!";
 }
```
{% endcode %}

</details>

<details>

<summary><a href="https://cplusplus.com/reference/string/string/rfind/">string :: findr() </a></summary>

{% code title="Find last occurrence of content in string" %}
```cpp
int main(){
    string haystack = "sadbutsad", needle = "sad";
    
    size_t found = haystack.rfind(needle);
    
    // Return the position of the first character of the last match.
    
    if(found != string::npos)
       cout << "Last Occurence of String needle in haystack: " << found;
}
```
{% endcode %}

</details>

<details>

<summary><a href="https://cplusplus.com/reference/string/string/find_first_of/">string :: find_first_of</a></summary>

{% code overflow="wrap" %}
```cpp
// string::find_first_of
#include <iostream>       // cout
#include <string>         // string
#include <cstddef>        // size_t
using namespace std;

int main () {
    string str ("Please, replace the vowels in this sentence by asterisks.");
    
    size_t found = str.find_first_of("aeiou");
    
    // Return the position of the first character that matches.
    
    while (found!=string::npos){
        str[found]='*';
        found=str.find_first_of("aeiou", found+1);
    }

    cout << str << '\n';

    return 0;
}
```
{% endcode %}

</details>

<details>

<summary><a href="https://cplusplus.com/reference/string/string/find_first_not_of/"><strong>string :: find_first_not_of</strong></a></summary>

{% code title="Find absence of character in string" %}
```cpp
// string::find_first_not_of
#include <iostream>       // cout
#include <string>         // string
#include <cstddef>        // size_t
using namespace std;

int main (){
    string str ("look for non-alphabetic characters...");

    size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
    
    // The position of the first character that does not match.
    // If no such characters are found, the function returns string::npos.

    if (found!=string::npos)
    {
        cout << "The first non-alphabetic character is " << str[found];
        cout << " at position " << found << '\n';
    }
    else
        cout << "Whole string contain Alphabetic Characters";
}
```
{% endcode %}

</details>

<details>

<summary><a href="https://cplusplus.com/reference/string/string/find_last_of/">string :: find_last_of</a></summary>

```cpp
// string::find_last_of
#include <iostream>       
#include <string>         
#include <cstddef>        
using namespace std;

void SplitFilename (string& str){
    cout << "Splitting: " << str << '\n';
    
    size_t found = str.find_last_of("/\\");
    
    cout << " path: " << str.substr(0,found) << '\n';
    
    cout << " file: " << str.substr(found+1) << '\n';
}

int main (){
    string str1 ("/usr/bin/man");
    string str2 ("c:\\windows\\delete");

    SplitFilename (str1);
    SplitFilename (str2);

    return 0;
}
```

</details>

<details>

<summary><a href="https://cplusplus.com/reference/string/string/find_last_not_of/">string::find_last_not_of</a></summary>

{% code title="" %}
```cpp
// string::find_last_not_of
#include <iostream>       
#include <string>         
#include <cstddef>        
using namespace std;

int main (){
    string str ("Please, erase trailing white-spaces   \n");
    string whitespaces (" \t\f\v\n\r");
   
    // Return the position of the first character that does not match.
    
    size_t found = str.find_last_not_of(whitespaces);
    if (found!=string::npos)
        str.erase(found+1);
    else
        str.clear();            // str is all whitespace

    cout << '[' << str << "]\n";

}
```
{% endcode %}

</details>

<details>

<summary><a href="https://cplusplus.com/reference/string/string/replace/">string::replace</a></summary>

{% code fullWidth="true" %}
```cpp
// replacing in a string
#include <iostream>
#include <string>
using namespace std;

int main (){
    
    string base="this is a test string.";
    string str2="n example";
    string str3="sample phrase";
    string str4="useful.";

    // replace signatures used in the same order as described above:

    // Using positions:                 0123456789*123456789*12345
    string str=base;           // "this is a test string."
    str.replace(9,5,str2);          // "this is an example string." (1)
    str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
    str.replace(8,10,"just a");     // "this is just a phrase."     (3)
    str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
    str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)

    // Using iterators:                                               0123456789*123456789*
    str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
    str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
    str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
    str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
    str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)
    
    cout << str << '\n';
}
```
{% endcode %}

</details>
