-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString-API.cpp
More file actions
72 lines (44 loc) · 1.82 KB
/
String-API.cpp
File metadata and controls
72 lines (44 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>
using namespace std;
// String API
// Link: https://youtu.be/lTPT1cPfVmI
// Title: C++ useful string functions
// Creator: Bro Code
// Non sono tutte: https://cplusplus.com/reference/string/string/
int main() {
setlocale(LC_ALL, "italian");
string first_name;
cout << "Inserisci il tuo nome: ";
getline(cin, first_name);
int lenght = first_name.length(); // lunghezza della stringa
if (lenght >= 12) // si può scrivere anche come first_name.lenght() >= 12
cout << "Il tuo nome non può essere più lungo di 12 caratteri!" << endl;
else
cout << "Benvenuto " << first_name << endl;
if (first_name.empty()) // verifica se la stringa è vuota (" " != vuoto)
cout << "Non hai inserito un nome! << endl";
// first_name.clear(); // serve per cancellare il contenuto della stringa
string email = first_name.append("@gmail.com"); // serve per aggingere una
// stringa ad un'altta ma
// si può usare anche +=
cout << email << endl;
char letter1 = first_name.at(0); // per "estrare la lettera" dalla stringa
// che a sua volta è un array di char
// quindi indice 0 = 1 lettera
cout << letter1 << endl;
string nickname = first_name.substr(0, 3); // sottrae un range di lettere
// dalla stringa (da, a)
cout << nickname << "123" << endl;
string ciao = first_name.insert(2, "hey@"); // aggiunge stringa ad un'naltra
// (dove, che cosa)
cout << ciao << endl;
int posizione = first_name.find('l'); // cerca la lettera una striga e
// restituisce il suo indice
cout << posizione << endl;
string phone_number;
cin >> phone_number;
phone_number.erase(0, 3); // cacella una parte della stringa (da, a)
cout << phone_number << endl;
return 0;
}