Han*_*ark 154
从 dart 2.6 版开始,dart 支持扩展:
extension StringExtension on String {
String capitalize() {
return "${this[0].toUpperCase()}${this.substring(1)}";
}
}
Run Code Online (Sandbox Code Playgroud)
所以你可以像这样调用你的扩展:
import "string_extension.dart";
var someCapitalizedString = "someString".capitalize();
Run Code Online (Sandbox Code Playgroud)
Gün*_*uer 43
main() {
String s = 'this is a string';
print('${s[0].toUpperCase()}${s.substring(1)}');
}
Run Code Online (Sandbox Code Playgroud)
Ale*_*uin 32
void main() {
print(capitalize("this is a string"));
// displays "This is a string"
}
String capitalize(String s) => s[0].toUpperCase() + s.substring(1);
Run Code Online (Sandbox Code Playgroud)
请参阅在DartPad上运行的此代码段:https://dartpad.dartlang.org/c8ffb8995abe259e9643
或者您可以使用strings包,请参阅capitalize.
Nit*_*Sai 22
超级晚了,但我用,
String title = "some string with no first letter caps";
title = title.replaceFirst(title[0], title[0].toUpperCase()); // Some string with no...
Run Code Online (Sandbox Code Playgroud)
Eph*_*rom 17
有一个涵盖此功能的 utils 包。它有一些更好的字符串操作方法。
安装它:
dependencies:
basic_utils: ^1.2.0
Run Code Online (Sandbox Code Playgroud)
用法 :
dependencies:
basic_utils: ^1.2.0
Run Code Online (Sandbox Code Playgroud)
GitHub:
https://github.com/Ephenodrom/Dart-Basic-Utils
Jen*_*enn 12
其他答案中的子字符串解析不考虑区域差异。程序包中的toBeginningOfSentenceCase()函数intl/intl.dart处理基本的句子大小写以及土耳其语和阿塞拜疆语中带点划线的“ i”。
import 'package:intl/intl.dart';
...
String sentence = toBeginningOfSentenceCase('this is a string'); // This is a string
Run Code Online (Sandbox Code Playgroud)
您可以在 flutter ReCase 中使用此包, 它为您提供各种大小写转换功能,例如:
CONSTANT_CASE
ReCase sample = new ReCase('hello world');
print(sample.sentenceCase); // Prints 'Hello world'
Run Code Online (Sandbox Code Playgroud)小智 8
正如 Ephenodrom 之前提到的,\n您可以在 pubspeck.yaml 中添加 basic_utils 包,并在 dart 文件中使用它,如下所示:
\nStringUtils.capitalize("yourString");\nRun Code Online (Sandbox Code Playgroud)\n这对于单个功能来说是可以接受的,但在更大的操作链中,这就变得很尴尬。
\n正如Dart 语言文档中所解释的:
\ndoMyOtherStuff(doMyStuff(something.doStuff()).doOtherStuff())\nRun Code Online (Sandbox Code Playgroud)\n该代码的可读性比以下代码要差得多:
\nsomething.doStuff().doMyStuff().doOtherStuff().doMyOtherStuff()\nRun Code Online (Sandbox Code Playgroud)\n该代码也更难被发现,因为 IDE 可以建议doMyStuff()after something.doStuff(),但不太可能建议将doMyOtherStuff(\xe2\x80\xa6)表达式放在周围。
出于这些原因,我认为你应该为 String 类型添加一个扩展方法(从 dart 2.6 开始就可以这样做!),如下所示:
\n/// Capitalize the given string [s]\n/// Example : hello => Hello, WORLD => World\nextension Capitalized on String {\n String capitalized() => this.substring(0, 1).toUpperCase() + this.substring(1).toLowerCase();\n}\nRun Code Online (Sandbox Code Playgroud)\n并使用点表示法调用它:
\n\'yourString\'.capitalized()\nRun Code Online (Sandbox Code Playgroud)\n或者,如果您的值可以为 null,则在调用中将点替换为 \'?.\':
\nmyObject.property?.toString()?.capitalized()\nRun Code Online (Sandbox Code Playgroud)\n
小智 8
如果您使用 get: ^4.6.5 作为 flutter 的状态管理,那么有用于大写的内置扩展
// This will capitalize first letter of every word
print('hello world'.capitalize); // Hello World
// This will capitalize first letter of sentence
print('hello world'.capitalizeFirst); // Hello world
// This will remove all white spaces from sentence
print('hello world'.removeAllWhitespace); // helloworld
// This will convert string to lowerCamelCase
print('This is new world'.camelCase); // thisIsNewWorld
// This will remove all white spaces between the two words and replace it with '-'
print('This is new world'.paramCase); // this-is-new-world
Run Code Online (Sandbox Code Playgroud)
String capitalize(String s) => (s != null && s.length > 1)
? s[0].toUpperCase() + s.substring(1)
: s != null ? s.toUpperCase() : null;
Run Code Online (Sandbox Code Playgroud)
它通过了测试:
test('null input', () {
expect(capitalize(null), null);
});
test('empty input', () {
expect(capitalize(''), '');
});
test('single char input', () {
expect(capitalize('a'), 'A');
});
test('crazy input', () {
expect(capitalize('?a!'), '?a!');
});
test('normal input', () {
expect(capitalize('take it easy bro!'), 'Take it easy bro!');
});
Run Code Online (Sandbox Code Playgroud)
小智 5
void allWordsCapitilize (String str) {
return str.toLowerCase().split(' ').map((word) {
String leftText = (word.length > 1) ? word.substring(1, word.length) : '';
return word[0].toUpperCase() + leftText;
}).join(' ');
}
allWordsCapitilize('THIS IS A TEST'); //This Is A Test
Run Code Online (Sandbox Code Playgroud)
String? toCapitalize(String? input) {
if (input == null || input.isEmpty) return input;
return '${input[0].toUpperCase()}${input.substring(1).toLowerCase()}';
}
Run Code Online (Sandbox Code Playgroud)
或扩展名:
extension StringExtension on String {
String? toCapitalize() {
if (this == null || this.isEmpty) return this;
return '${this[0].toUpperCase()}${this.substring(1).toLowerCase()}';
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15799 次 |
| 最近记录: |