THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This tutorial shows you multiple ways to get the First and Last Letters of a Given input string.
The string is a group of characters enclosed in single or double quotes.
There are multiple ways we can get the first letter of a string.
string[0] gives the character at the first position Here is an example
void main() {
String name = 'john';
print(name[0]); //n
}
void main() {
String name = 'john';
print('${name[0]}'); // j
}
String substring
method returns the substring of a starting and optional end index.
String substring(int start, [int? end])
void main() {
String name = 'john';
print(name.substring(0,1)); //j
}
index
of string length:
name[name.length - 1] returns the last element
Here is an example programvoid main() {
String name = 'john';
print(name[name.length - 1]); //n
}
Second-way using interpolation
syntax with index.
void main() {
String name = 'john';
print('${name[name.length-1]}'); // n
}
The third way, using the substring
method as given below
void main() {
String name = 'john';
print(name.substring(name.length-1)); //n
}
Learn how to get the first and last letter of a given string in dart and flutter
🧮 Tags
Recent posts
Julia examples - Variable Type Nim example - Convert String to/from the Int How to get length of an array and sequence in Nim? Nim environment variables - read, set, delete, exists, and iterate examples? How to convert from single character to/from string in Nim?Related posts