Strings Example Program in Dart


This code is also written in the Dart programming language and defines a main() function that demonstrates some string manipulation functions in Dart. Here's a breakdown of what's happening in this code:

  • String name1='Tutor';: This line declares a string variable name1 and initializes it with a value of "Tutor".
  • print(name1);: This line outputs the value of name1 to the console output using the print() function.
  • String name2='Joes'; : This line declares another string variable name2 and initializes it with a value of "Joes".
  • print(name2);: This line outputs the value of name2 to the console output using the print() function.
  • print("Hi ${name1} ${name2}");: This line uses string interpolation to output a greeting message to the console output. The values of name1 and name2 are included in the message using ${} syntax.
  • String name="Tutor Joes ";: This line declares a string variable name and initializes it with a value of "Tutor Joes ".
  • print(name.substring(0,5));: This line uses the substring() function to extract the first 5 characters from name and outputs the result to the console output.
  • int index=name.indexOf(' '); : This line uses the indexOf() function to find the index of the first occurrence of a space character in name and assigns the result to a new variable index.
  • print(index);: This line outputs the value of index to the console output using the print() function.
  • print(name.substring(index).trim());: This line uses the substring() function again, this time to extract the substring starting from the first space character in name and removes any leading or trailing white space using the trim() function. The result is then output to the console output.
  • print(name.toUpperCase());: This line uses the toUpperCase() function to convert all characters in name to uppercase and outputs the result to the console output.
  • print(name.toLowerCase());: This line uses the toLowerCase() function to convert all characters in name to lowercase and outputs the result to the console output.
  • print(name.length); : This line uses the length property to determine the number of characters in name and outputs the result to the console output.
  • print(name.contains('xyz')); : This line uses the contains() function to check if the string "xyz" exists in name and outputs the result (false in this case) to the console output.
  • name="Tutor Joes Computer Education";: This line assigns a new value to the name variable.
  • List words=name.split(' ');: This line uses the split() function to split name into a list of substrings separated by space characters and assigns the result to a new list variable words.
  • print(words);: This line outputs the words list to the console output.
  • print(words[0]);: This line outputs the first element of the words list (which is "Tutor" in this case) to the console output.

Source Code

void main() {
   String name1='Tutor';
   print(name1);
   String name2='Joes';
   print(name2);
   print("Hi ${name1} ${name2}");
   String name="Tutor Joes ";
   print(name.substring(0,5));
   int index=name .indexOf(' ');
   print(index);
   print(name.substring(index).trim());
   print(name.toUpperCase());
   print(name.toLowerCase());
   print(name.length);
   print(name.contains('xyz'));
   name="Tutor Joes Computer Education";
   List words=name.split(' ');
   print(words);
   print(words[0]);

}
To download raw file Click Here

Output

Tutor
Joes
Hi Tutor Joes
Tutor
5
Joes
TUTOR JOES 
tutor joes 
11
false
[Tutor, Joes, Computer, Education]
Tutor