Conteúdo do Curso
Introduction to Dart
Introduction to Dart
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.
main
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:
main
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 aString
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:
main
void main() { String word = 'CodeFinity'; print(word.toLowerCase()); }
toUpperCase()
: This method converts all characters in aString
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:
main
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
Obrigado pelo seu feedback!