如何在Flutter中将所有单词的首字母大写?

huangapple go评论104阅读模式
英文:

how to capitalized all words first letter to capital in flutter

问题

我已经用我的逻辑创建了一个字符串扩展。

我之前在stackoverflow上查看了以前提出的问题,但是在这里我想实现我的代码而不是遵循其他逻辑...

这是我的努力

extension StringExtension on String {
  String capitalize() {
    //将字符串的第一个字母大写
    if(trim().isEmpty)
      return '';
    return "${this[0].toUpperCase()}${this.substring(1)}";
  }

  String capitalizeEachWord(){
    //寻找好的代码
  }

  String removeExtraSpaces() {
    //删除单词之间的所有额外空格
    if(trim().isEmpty)
      return '';
    return trim().replaceAll(RegExp(' +'), ' ');
  }
}

这是我的void main()函数

String str = '  i   love  flutter   ';
print(str.removeExtraSpaces().capitalizeEachWord());
// 期望输出:I Love Flutter
英文:

I have created a String Extension with my logic.

I checked previously asked questions in stack overflow, but here I want to implement my code rather than following other logic...

here is my effort

extension StringExtension on String {
  String capitalize() {
    //to capitalize first letter of string to uppercase
    if(trim().isEmpty)
      return '';
    return "${this[0].toUpperCase()}${this.substring(1)}";
  }


String capitalizeEachWord(){
// looking for good code
}


  String removeExtraSpaces() {
    //to remove all extra spaces between words
    if(trim().isEmpty)
      return '';
    return trim().replaceAll(RegExp(' +'), ' ');
  }



}

here is my void main()

String str='  i   love  flutter   ';
print(str.removeExtraSpaces().capitalizeEachWord());
// want output : I Love Flutter

答案1

得分: 1

你可以尝试使用我的扩展代码,以便获得所需的输出:

扩展代码:

extension StringExtenssion on String {
  String removeExtraSpaces() {
    return replaceAll(RegExp(r'\s+'), ' ').trim();
  }

  String capitalizeWord() {
    return split(' ').map((word) {
      if (word.isEmpty) return '';
      return '${word[0].toUpperCase()}${word.substring(1).toLowerCase()}';
    }).join(' ');
  }
}

使用方法:

 String str = '  i   love  flutter   ';
 print("${str.removeExtraSpaces().capitalizeWord()}");

输出结果:I Love Flutter

英文:

You can try my Extension code where you can able to get needed output:

Extension code:

extension StringExtenssion on String {
  String removeExtraSpaces() {
    return replaceAll(RegExp(r'\s+'), ' ').trim();
  }

  String capitalizeWord() {
    return split(' ').map((word) {
      if (word.isEmpty) return '';
      return '${word[0].toUpperCase()}${word.substring(1).toLowerCase()}';
    }).join(' ');
  }
}

Use like this:

 String str = '  i   love  flutter   ';
 print("${str.removeExtraSpaces().capitalizeWord()}");

Output: I Love Flutter

答案2

得分: 1

要将每个单词的首字母大写,可以通过迭代它们并将首字母大写,然后添加单词的其余部分来实现。你可以使用split方法来实现。实现代码如下:

extension Capitalize on String {
  String capitalize() {
    if (trim().isNotEmpty) {
      final words = removeExtraSpaces()
        .split(' ')
        .map((e) => e[0].toUpperCase() + (e.length > 1 ? e.substring(1) : ''))
        .toList();
      return words.join(' ');
    }
    else return this;
  }
  String removeExtraSpaces() {
    if(trim().isEmpty) return '';
    return trim().replaceAll(RegExp(' +'), ' ');
  }
}

void main() {
  final str = 'i    love   flutter    ';
  print(str.capitalize());
}
英文:

To capitalize every word, iterate through them and capitalize first letter and add the rest of the word. You can do so with split method. The implementation would look something like this:

extension Capitalize on String {
  String capitalize() {
    if (trim().isNotEmpty) {
      final words = removeExtraSpaces()
        .split(' ')
        .map((e) => e[0].toUpperCase() + (e.length > 1 ? e.substring(1) : ''))
        .toList();
      return words.join(' ');
    }
    else return this;
  }
  String removeExtraSpaces() {
    if(trim().isEmpty) return '';
    return trim().replaceAll(RegExp(' +'), ' ');
  }
}

void main() {
  final str = 'i    love   flutter    ';
  print(str.capitalize());
}

huangapple
  • 本文由 发表于 2023年8月9日 17:17:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定