句号“.”后第一个单词的TextField值首字母如何大写?
How to capitalize the TextField value first letter of the first word after full stop "."?
这种事情可能吗?
示例:“我最喜欢的电影是黑客帝国。而且我认为...”
之前也有人问过类似的问题,但都是简单的大写第一个单词的第一个字母,而不是更多
此代码仅适用于第一个单词的第一个字母,无法理解“.”
extension CapExtension on String {
String get inCaps =>
this.length > 0 ? '${this[0].toUpperCase()}${this.substring(1)}' : '';
String get capitalizeFirstofEach => this
.replaceAll(RegExp(' +'), ' ')
.split(" ")
.map((str) => str.inCaps)
.join(" ");
}
class CapitalCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.inCaps,
selection: newValue.selection,
);
}
}
TextFormField(
inputFormatters: [
CapitalCaseTextFormatter()
]
)
添加textCapitalization
属性
TextFormField(
textCapitalization: TextCapitalization.sentences,
inputFormatters: [
CapitalCaseTextFormatter() //no need of it then
]
)
使用这个将每个句子的第一个单词大写
extension CapExtension on String {
String capitalizeSentence() {
// Each sentence becomes an array element
var sentences = this.split('.');
// Initialize string as empty string
var output = '';
// Loop through each sentence
for (var sen in sentences) {
// Trim leading and trailing whitespace
var trimmed = sen.trim();
// Capitalize first letter of current sentence
var capitalized = "${trimmed[0].toUpperCase() + trimmed.substring(1)}";
// Add current sentence to output with a period
output += capitalized + ". ";
}
return output;
}
}
// 文本输入格式化程序
class CapitalCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.capitalizeSentence,
selection: newValue.selection,
);
}
}
这种事情可能吗?
示例:“我最喜欢的电影是黑客帝国。而且我认为...”
之前也有人问过类似的问题,但都是简单的大写第一个单词的第一个字母,而不是更多
此代码仅适用于第一个单词的第一个字母,无法理解“.”
extension CapExtension on String {
String get inCaps =>
this.length > 0 ? '${this[0].toUpperCase()}${this.substring(1)}' : '';
String get capitalizeFirstofEach => this
.replaceAll(RegExp(' +'), ' ')
.split(" ")
.map((str) => str.inCaps)
.join(" ");
}
class CapitalCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.inCaps,
selection: newValue.selection,
);
}
}
TextFormField(
inputFormatters: [
CapitalCaseTextFormatter()
]
)
添加textCapitalization
属性
TextFormField(
textCapitalization: TextCapitalization.sentences,
inputFormatters: [
CapitalCaseTextFormatter() //no need of it then
]
)
使用这个将每个句子的第一个单词大写
extension CapExtension on String {
String capitalizeSentence() {
// Each sentence becomes an array element
var sentences = this.split('.');
// Initialize string as empty string
var output = '';
// Loop through each sentence
for (var sen in sentences) {
// Trim leading and trailing whitespace
var trimmed = sen.trim();
// Capitalize first letter of current sentence
var capitalized = "${trimmed[0].toUpperCase() + trimmed.substring(1)}";
// Add current sentence to output with a period
output += capitalized + ". ";
}
return output;
}
}
// 文本输入格式化程序
class CapitalCaseTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
return TextEditingValue(
text: newValue.text.capitalizeSentence,
selection: newValue.selection,
);
}
}