如何在 dart 中将字符串的第一个字母大写? [英] How to capitalize the first letter of a string in dart?
本文介绍了如何在 dart 中将字符串的第一个字母大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将字符串的第一个字符大写,同时不改变任何其他字母的大小写?
How do I capitalize the first character of a string, while not changing the case of any of the other letters?
例如,这是一个字符串"应该给出这是一个字符串".
For example, "this is a string" should give "This is a string".
推荐答案
从 dart 2.6 版本开始,dart 支持扩展:
Since dart version 2.6, dart supports extensions:
extension StringExtension on String {
String capitalize() {
return "${this[0].toUpperCase()}${this.substring(1)}";
}
}
所以你可以像这样调用你的分机:
So you can just call your extension like this:
import "string_extension.dart";
var someCapitalizedString = "someString".capitalize();
这篇关于如何在 dart 中将字符串的第一个字母大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文