如何将字符串的第一个字符大写,同时不改变其他字母的大小写呢?
例如,"this is a string" 应该转换为 "This is a string"。
如何将字符串的第一个字符大写,同时不改变其他字母的大小写呢?
例如,"this is a string" 应该转换为 "This is a string"。
自 Dart 2.6 版本以来,Dart 支持扩展:
extension StringExtension on String {
String capitalize() {
return "${this[0].toUpperCase()}${this.substring(1).toLowerCase()}";
}
}
那么你只需要像这样调用你的扩展:
import "string_extension.dart";
var someCapitalizedString = "someString".capitalize();
复制这段内容到某个地方:
extension StringCasingExtension on String {
String toCapitalized() => length > 0 ?'${this[0].toUpperCase()}${substring(1).toLowerCase()}':'';
String toTitleCase() => replaceAll(RegExp(' +'), ' ').split(' ').map((str) => str.toCapitalized()).join(' ');
}
用法:
// import StringCasingExtension
final helloWorld = 'hello world'.toCapitalized(); // 'Hello world'
final helloWorld = 'hello world'.toUpperCase(); // 'HELLO WORLD'
final helloWorldCap = 'hello world'.toTitleCase(); // 'Hello World'
str.capitalize
жІЎжњ‰е®љд№‰гЂ‚ж‰Ђд»ҐдЅ еЏЇд»ҐдЅїз”Ёstr.inCaps
гЂ‚ - AshanUncaught Error: RangeError (index): Index out of range: no indices are valid: 0
- BIS Techfinal helloWorld = 'hello world'.capitalizeFirstofEach;
- BIS Tech其他答案中的子字符串解析并没有考虑本地化差异。
intl/intl.dart
包中的toBeginningOfSentenceCase()函数处理基本的句子大小写和土耳其语和阿塞拜疆语中的点缀符号“i”。
import 'package:intl/intl.dart' show toBeginningOfSentenceCase;
print(toBeginningOfSentenceCase('this is a string'));
void main() {
print(capitalize("this is a string"));
// displays "This is a string"
}
String capitalize(String s) => s[0].toUpperCase() + s.substring(1);
在DartPad上查看此代码片段:https://dartpad.dartlang.org/c8ffb8995abe259e9643
s[0].toUpperCase() + s.substring(1).toLowerCase();
来将其转化为首字母大写、其余字母小写的格式。 - TomTom101有一个utils包涵盖了该函数。它还有一些更好的操作字符串的方法。
安装方法如下:
dependencies:
basic_utils: ^1.2.0
使用方法:
String capitalized = StringUtils.capitalize("helloworld");
Github:
虽然有点晚了,但我使用的是,
String title = "some string with no first letter caps";
title = title.replaceFirst(title[0], title[0].toUpperCase()); // Some string with no...
您可以在Flutter中使用此软件包 ReCase 它为您提供各种大小写转换功能,例如:
CONSTANT_CASE
ReCase sample = new ReCase('hello world');
print(sample.sentenceCase); // Prints 'Hello world'
String mockText = 'This is-Some_sampleText. YouDig?';
- WillHaslett如Ephenodrom之前所述,您可以在pubspeck.yaml中添加basic_utils包,并在dart文件中像这样使用它:
StringUtils.capitalize("yourString");
doMyOtherStuff(doMyStuff(something.doStuff()).doOtherStuff())
那段代码比以下的代码难以阅读得多:
something.doStuff().doMyStuff().doOtherStuff().doMyOtherStuff()
something.doStuff()
之后建议 doMyStuff()
,但不太可能建议在表达式周围放置 doMyOtherStuff(…)
。出于这些原因,我认为你应该像这样为String类型添加一个扩展方法(你可以在dart 2.6中这样做!):/// Capitalize the given string [s]
/// Example : hello => Hello, WORLD => World
extension Capitalized on String {
String capitalized() => this.substring(0, 1).toUpperCase() + this.substring(1).toLowerCase();
}
'yourString'.capitalized()
如果你的值可以为空,那么在调用中用问号'?. '替换点号:
myObject.property?.toString()?.capitalized()
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
Uncaught Error: RangeError (index): Index out of range: no indices are valid: 0
- BIS TechString data = allWordsCapitilize('THIS IS A TEST') ;
- BIS Tech // 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
return "${this[0].toUpperCase()}${this.substring(1).toLowerCase()}";
。如果不这样做,它会正确地将 'this' 大写,但不会将 'THIS' 大写。 - Luciano Rodríguezif (isEmpty) return this
。 - Venkat D.if (this == null || this == "") return "";
- Maciej