metadeck
10/5/2018 - 8:53 AM

Beginners Dart - Strings

Beginners Dart - Strings

void main() {

  //Here we have defined a String named John Smith
  String name = "John Smith";
  
  //Here we printed the name
  print(name);

  //Here we created a string concatenation with a dollar sign and brackets, the program will now print Hello John Smith 
  print("Hello ${name}.");

  //So here we assigned a substring to line 4 letters in.
  String firstName = name.substring(0,4);

  //Here we created a string concatenation that outputs First name = John.
  print("First name = ${firstName}.");

  //This will look for the specified letter in the string and add it and the letters after it to the string.
  int index = name.indexOf(" ");

  //This line trims the space from the new string and then assigns it to the string lastName.
  String lastName = name.substring(index).trim();

  //This line will now create a concatenation of the string and will output Last name = Smith.
  print("Last name = ${lastName}.");

  //This will return an integer value specifying the amount of characters in the string.
  print(name.length);

  //This will search the string for the contained text, it will then return a boolean value of true or false.
  print(name.contains("S"));

  //This will create a list that will split the variable at each interval that it finds the contained character or string.
  List parts = name.split(" ");

  //Displays the first list item.
  print(parts[0]);

  //Displays the second list item.
  print(parts[1]);

}