Skip to content

Instantly share code, notes, and snippets.

@abdorll
Created October 8, 2023 19:27
Show Gist options
  • Save abdorll/ce85611ad19088d349ecebe247e191c9 to your computer and use it in GitHub Desktop.
Save abdorll/ce85611ad19088d349ecebe247e191c9 to your computer and use it in GitHub Desktop.
void main() {
// ==================BASIC USAGE 1.
String singleQ = 'Single quotes';
String doubleQ ="Double quotes";
String doubleQtInSingQt = 'Double quotes in "single" quotes';
String singleQtInDblQt = "Single quotes in 'double' quotes";
String multilineOne='''A
multiline
string''';
String multiLine2 ="""Another
multiline
string""";
// ==================BASIC USAGE 2.
String name = "Abdullah";
String greeting = "Hello,";
String message = 'Dart is fun!';
String? nullableString;
nullableString = "I can be null.";
var dynamicString = "I'm dynamic.";
// ==================STRING INTERPOLATION
String fruit = "apple";
int count = 3;
String fruitSentence = 'I have $count ${fruit}s.';
// ==================STRING CONCATENATION
String fullName = name + " Opadeji";
String combinedMessage = greeting + " " + message;
String interpolatedMessage = '$greeting $name, $message';
// ==================STRING PROPERTIES
String text = "Dart is versatile.";
// 1. length → int (Returns the length of the string)
int stringLength = text.length;
// 2. isEmpty → bool (Returns true if the string is empty)
bool isEmpty = text.isEmpty;
// 3. isNotEmpty → bool (Returns true if the string is not empty)
bool isNotEmpty = text.isNotEmpty;
// 4. runtimeType → Type (A representation of the runtime type of the object)
Type runtimeType = text.runtimeType;
// ==================STRING METHODS
String sentence = "Dart programming is easy.";
// 1. toUpperCase() → String (Converts the string to uppercase)
String upperCaseSentence = sentence.toUpperCase();
// 2. toLowerCase() → String (Converts the string to lowercase)
String lowerCaseSentence = sentence.toLowerCase();
// 3. contains() → bool (Checks if the string contains a substring)
bool containsWord = sentence.contains("programming");
// 4. substring() → String (Extracts a portion of the string)
String extracted = sentence.substring(5, 16);
// 5. split() → List<String> (Splits the string into a list of substrings)
List<String> words = sentence.split(" "); // "Hello"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment