how to get substring code example
Example 1: substring javascript
var str = "Hello world!";
var res = str.substring(1, 4); //ell
Example 2: how to do substring java
class Main {
public static void main (String[] args) {
String str = "Hello World!";
String firstWord = str.substring(0, 5);
//two parameters are start and end index: (inclusive, non-inclusive)
String secondWord = str.substring(6, 11);
//firstWord has string "Hello"
//secondWord has string "World"
}
}
Example 3: str.substring if 2 letters
public String firstTwo(String str) {
return str.length() < 2 ? str : str.substring(0, 2);
}