Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
What are Methods? | List and String
Introduction to Dart
course content

Course Content

Introduction to Dart

Introduction to Dart

1. First Acquaintance with Dart
2. Variables and Data Types
3. Conditional Statements
4. List and String
5. Loops

book
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, the List was an object, and add() and remove() 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:

dart

main

copy
1234567
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?

Choose the correct option

Choose the correct option

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 4
We're sorry to hear that something went wrong. What happened?
some-alt