Home > Software engineering >  Is there any method like char At in Dart?
Is there any method like char At in Dart?

Time:07-17

String name = "Jack";  
char letter = name.charAt(0);    
System.out.println(letter);

You know this is a java method charAt that it gives you a character of a String just by telling the index of the String. I'm asking for a method like this in Dart, does Dart have a method like that?

CodePudding user response:

You can use String.operator[].

String name = "Jack";  
String letter = name[0];
print(letter);

Note that this operates on UTF-16 code units, not on grapheme clusters. Also note that Dart does not have a char type.

If you need to operate on arbitrary Unicode strings, then you should use package:characters and do:

String name = "Jack";  
Characters letter = name.characters.characterAt(0);
print(letter);

CodePudding user response:

You can use

String.substring(int startIndex, [ int endIndex ])

Example --

void main(){
   String s = "hello";
   print(s.substring(1, 2));
}

Output

e

Note that , endIndex is one greater than startIndex, and the char which is returned is present at startIndex.

  • Related