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

Conteúdo do Curso

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
String Properties and Methods

What is the Difference Between Method and Property ?

Methods are like operations or actions you can perform on an object. For example, think of a ball. When you kick the ball, that's an action - a method. You're doing something with the ball.

Properties are like characteristics of an object. For example, the color of the ball or its size. Properties describe the object but don't perform actions on it.

So, methods are actions, and properties are characteristics. In the case of String, methods, and properties can be used to work with text.

String properties

  • length - Returns the length of the string including space.
dart

main

copy
1234
void main() { String word = 'Codefinity'; print(word.length); }

First, a variable word is created, and it's assigned the String 'Codefinity'.

Then, the length property is called on the word String. This property counts the number of characters (letters) in the String and returns that value.

The obtained value (the number of characters in the 'Codefinity' String, which is 10) is passed to the print() function for output.

As a result, the number 10 is displayed on the screen, indicating the number of characters in the 'Codefinity' String.

  • The isEmpty method is used to check if a string is empty, meaning it doesn't contain any characters:
dart

main

copy
1234567
void main() { String word = 'Codefinity'; print(word.isEmpty); // `false` String line = ''; print(line.isEmpty); // `true` }

So, the isEmpty method helps to check if a String is empty, returning true if the string is empty or false if the String contains at least one character. This is useful for determining whether certain actions should be taken based on whether a String is empty or not.

Strings Methods

  • toLowerCase(): This method converts all characters in a String to lowercase. It allows you to ensure a consistent lowercase representation of characters, which can be useful when comparing two strings regardless of their case or when you want the text to be consistently in lowercase:
dart

main

copy
1234
void main() { String word = 'CodeFinity'; print(word.toLowerCase()); }
  • toUpperCase(): This method converts all characters in a String to uppercase. It's used to create text where all letters are in uppercase, which can be helpful when you want to display text in a title-like format or make the text more prominent:
dart

main

copy
1234
void main() { String word = 'Codefinity'; print(word.toUpperCase()); }

So, the toLowerCase() and toUpperCase() methods allow you to easily manipulate the case of characters in strings, making them valuable in various programming and text processing scenarios.

Task

question-icon

Convert all characters of the string "word" to uppercase.

print();

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 5
We're sorry to hear that something went wrong. What happened?
some-alt