Skip to content

Instantly share code, notes, and snippets.

@leafney
Last active May 11, 2021 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leafney/f2bfd6d5d1c8c190597c35c759c21361 to your computer and use it in GitHub Desktop.
Save leafney/f2bfd6d5d1c8c190597c35c759c21361 to your computer and use it in GitHub Desktop.
void main() {
String? ss;
print(ss);
// ss = " ";
// ss="hello";
print(ss);
print('isNullOrEmpty: ${ss.isNullOrEmpty()}');
print('isNotNullAndEmpty: ${ss.isNotNullAndEmpty()}');
print('getStrValue: ${ss.getStrValue(val: "x")}');
print('-------');
print('StrUtils.isNullOrEmpty: ${StrUtils.isNullOrEmpty(ss)}');
print('StrUtils.isNotNullAndEmpty: ${StrUtils.isNotNullAndEmpty(ss)}');
print('StrUtils.getStrValue: ${StrUtils.getStrValue(ss)}');
}
// use Extension
extension StringExtensions on String? {
/// string is null or empty
bool isNullOrEmpty() => this == null || this!.trim().isEmpty;
/// string not null and empty
bool isNotNullAndEmpty() => this != null && this!.trim().isNotEmpty;
/// if string is null or empty ,return default value
// String getNullOrEmptyVal({String val = ""}) {
// if(this.isNullOrEmpty()){
// return val;
// }
// return this!;
// }
String getStrValue({String val = ""}) => this.isNullOrEmpty() ? val : this!;
}
// use Function
class StrUtils {
StrUtils._();
/// string is null or empty
static bool isNullOrEmpty(String? s) => s == null || s.trim().isEmpty;
/// string not null and empty
static bool isNotNullAndEmpty(String? s) => s != null && s.trim().isNotEmpty;
/// if string is null or empty ,return default value
static String getStrValue(String? s, {String val = ""}) =>
isNullOrEmpty(s) ? val : s!;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment