Course Content
Introduction to Dart
Introduction to Dart
What are Methods?
Methods
Object: In programming, an object is like an object in the real world, such as a car or a book. Objects have characteristics and can perform various actions.
Method: A method is an action or function that an object can perform. For example, an "car" object might have methods like "accelerate" or "brake."
Calling a Method: To make an object perform its method, you specify the method's name and may provide some data if needed. For instance, to make a car accelerate, you call the "accelerate" method on the car.
So, a method is an action that an object can do, and you call this action to have the object perform it.
Note
We have already used methods when adding or removing elements from a
List
. Back then, theList
was an object, andadd()
andremove()
were methods.
The syntax for working with methods:
- Parentheses
()
serve as a way to tell the program that you want to execute (run) that particular method. They indicate that you intend to carry out the actions defined within the method. - Parentheses allow you to pass necessary values or arguments to the method, which can be used inside the method. Arguments are data that the method may need to perform specific operations or calculations.
Example
You can use the substring()
method to extract substrings from a String
. This method takes two values: the substring's start and the substring's end. The beginning of the substring is the index of the first character of the substring, and the end of the substring is the index of the character that follows the last character of the substring.
For example, to extract a substring "Hello" from the string "Hello, world!". You can use the following code:
main
void main(){ String str = "Hello, world!"; // Extract a substring from the string String ourSubstring = str.substring(0, 5); // Print the substring print(ourSubstring); }
Task
How to extract the substring "Programming"
from the String
str?
Thanks for your feedback!