c++ string substring code example
Example 1: std::substring
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
std::string str2 = str.substr (3,5);
std::size_t pos = str.find("live");
std::string str3 = str.substr (pos);
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}
Example 2: substring javascript
var str = "Hello world!";
var res = str.substring(1, 4);
Example 3: substring
const str = 'Mozilla';
console.log(str.substring(1, 3));
console.log(str.substring(2));
Example 4: str.substring if 2 letters
public String firstTwo(String str) {
return str.length() < 2 ? str : str.substring(0, 2);
}