Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first letter of string in flutter dart?

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.

like image 991
Ankit Shah Avatar asked Aug 30 '19 16:08

Ankit Shah


People also ask

How do I get the first letter of a string to a string?

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.


1 Answers

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 
like image 57
Jorgesys Avatar answered Sep 28 '22 05:09

Jorgesys