Dart String toUpperCase()
Syntax & Examples
Syntax of String.toUpperCase()
The syntax of String.toUpperCase() method is:
String toUpperCase()
This toUpperCase() method of String converts all characters in this string to upper case.
Return Type
String.toUpperCase() returns value of type String
.
✐ Examples
1 Convert a string to uppercase
In this example,
- We create a string
str
with the value 'Hello, World!'. - We then use the
toUpperCase()
method to convert all characters in the string to uppercase. - The uppercase version of the string is stored in
upperCaseStr
. - We print both the original and uppercase strings to standard output.
Dart Program
void main() {
String str = 'Hello, World!';
String upperCaseStr = str.toUpperCase();
print('Original String: $str');
print('Uppercase String: $upperCaseStr');
}
Output
Original String: Hello, World! Uppercase String: HELLO, WORLD!
2 Convert another string to uppercase
In this example,
- We create a string
str
with the value 'abcdef'. - We then use the
toUpperCase()
method to convert all characters in the string to uppercase. - The uppercase version of the string is stored in
upperCaseStr
. - We print both the original and uppercase strings to standard output.
Dart Program
void main() {
String str = 'abcdef';
String upperCaseStr = str.toUpperCase();
print('Original String: $str');
print('Uppercase String: $upperCaseStr');
}
Output
Original String: abcdef Uppercase String: ABCDEF
3 Convert a sentence to uppercase
In this example,
- We create a string
str
with the value 'Lorem ipsum dolor sit amet'. - We then use the
toUpperCase()
method to convert all characters in the string to uppercase. - The uppercase version of the string is stored in
upperCaseStr
. - We print both the original and uppercase strings to standard output.
Dart Program
void main() {
String str = 'Lorem ipsum dolor sit amet';
String upperCaseStr = str.toUpperCase();
print('Original String: $str');
print('Uppercase String: $upperCaseStr');
}
Output
Original String: Lorem ipsum dolor sit amet Uppercase String: LOREM IPSUM DOLOR SIT AMET
Summary
In this Dart tutorial, we learned about toUpperCase() method of String: the syntax and few working examples with output and detailed explanation for each example.