How do I get the first character of a string in dart flutter.
For example,a string having a value "Hello World" should return only "H". I am fetching data from my firestore database. My code is:
searchByName(String searchField) { return Firestore.instance .collection('posts') .where('description', isEqualTo: searchField.substring(0, 1).toUpperCase()) .getDocuments(); }
I want to get the first letter from the data that I recieve from 'description' field in above code.
To get the first and last characters of a string, access the string at the first and last indexes. For example, str[0] returns the first character, whereas str[str. length - 1] returns the last character of the string.
You can do this by getting the first element of the String (indicated by index 0):
'${mystring[0]}'
example:
String mystring = 'Hello World'; print('${mystring[0]}');
in this case you will get as an output:
H
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With