我如何大写字符串的第一个字符,而不改变任何其他字母的情况?

例如,“this is a string”应该给出“this is a string”。


当前回答

这是使用String类方法splitMapJoin在dart中大写字符串的另一种选择:

var str = 'this is a test';
str = str.splitMapJoin(RegExp(r'\w+'),onMatch: (m)=> '${m.group(0)}'.substring(0,1).toUpperCase() +'${m.group(0)}'.substring(1).toLowerCase() ,onNonMatch: (n)=> ' ');
print(str);  // This Is A Test 

其他回答

你可以使用Text_Tools包,使用简单:

https://pub.dev/packages/text_tools

你的代码应该是这样的:

//This will print 'This is a string
print(TextTools.toUppercaseFirstLetter(text: 'this is a string'));
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

最简单的答案是:

首先使用下标将字符串的第一个字母大写,然后将字符串的其余部分拼接起来。

这里username是字符串。

用户名[0].toUpperCase() + username.substring(1);

简单,没有任何扩展:

title = "some title without first capital"

title.replaceRange(0, 1, title[0].toUpperCase())

// Result: "Some title without first capital"

非常晚,但是我用,


String title = "some string with no first letter caps";
    
title = title.replaceFirst(title[0], title[0].toUpperCase()); // Some string with no...