Last active
September 2, 2021 19:54
-
-
Save emanuel-braz/e96d714404956680711e172a0966302e to your computer and use it in GitHub Desktop.
capitalizar string nome capitalize
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void main() { | |
var result = capitalize('minha_string_Capitalizada_coRreTamenTE', pattern: '_', firstCap: false); | |
print(result); | |
} | |
String capitalize(String value, {String? pattern, bool firstCap = false}) { | |
var wordList = value.replaceAll(RegExp(' +'), pattern ?? ' ').split(pattern ?? " "); | |
if (firstCap) { | |
return wordList.map((str) => capitalizeAll(str)).join(pattern ?? " ").replaceAll(pattern ?? ' ', ''); | |
} else { | |
var caps = wordList.sublist(1).map((str) => capitalizeAll(str)).join(pattern ?? " "); | |
return '${wordList[0]}$caps'.replaceAll(pattern ?? ' ', ''); | |
} | |
} | |
String capitalizeAll(String value, {bool fixOtherLetters = false}) { | |
if (fixOtherLetters){ | |
return value.length > 0 ?'${value[0].toUpperCase()}${value.substring(1).toLowerCase()}':''; | |
} else { | |
return value.length > 0 ?'${value[0].toUpperCase()}${value.substring(1)}':''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment