first method
- int n = 1
Returns the first n characters of the String.
n is optional, by default it returns the first character of the String.
- If
nprovided is longer than theString's length, the string will be returned. - If
nis negative, it will return the string without the firstncharacters.
Example 1
String foo = 'hello world';
String firstChars = foo.first(); // returns 'h'
Example 2
String foo = 'hello world';
bool firstChars = foo.first(3); // returns 'hel'
Implementation
String first([int n = 1]) {
if (n < 0) {
n = length + n;
}
if (n <= 0) {
return "";
}
if (isBlank || length < n) {
return blankIfNull;
}
return substring(0, n);
}