简体   繁体   English

在 dart-flutter 中将字符串拆分为字母

[英]split a String into letters in dart-flutter

i want to split a word into characters.我想把一个词分成字符。 "Amir Abbas" it contains one space. “Amir Abbas”它包含一个空格。 i use:我用:

List<String> singleWord = string.trim().split("");

but final result is:但最终结果是:

A, m, i, r, " ", "", A, b, b, a, s

what is "" character?什么是“”字符? how to exclude it?如何排除它? I use it in flutter and ios emulator.我在 flutter 和 ios 仿真器中使用它。

The following code does not give your result.以下代码没有给出您的结果。 It is not splitting an empty character for me.它不是为我分裂一个空字符。 Try typing your string again, eventually you might have a hidden illegal character?再次尝试输入你的字符串,最终你可能有一个隐藏的非法字符?

void main() {
  List<String> singleWord = "Amir Abbas".trim().split("");
  singleWord.forEach((element){
    print(element);
  });
}

在此处输入图像描述

You can filter the list with the where method and exclude the empty spaces您可以使用where方法过滤列表并排除空格

void main() {
  List<String> a = ["a", "b", ""];
  List<String> b = a.where((e) => e != "").toList();
  print(a); // ["a", "b", ""]
  print(b); // ["a", "b"]
}

It is working perfectly for me in dart pad and in my application.Run the application again and can u submit the output again它在 dart 垫和我的应用程序中非常适合我。再次运行应用程序,你可以再次提交 output

在此处输入图像描述 Try this approach by replacing trim() and just using split().通过替换 trim() 并仅使用 split() 来尝试这种方法。 You can alter the below code according to your need您可以根据需要更改以下代码

var names = "Amir and Abbas";
  List<String> namesSplit = names.split(" ");
  
  for(var i=0;i<namesSplit.length;i++){
    for(var j=0; j<namesSplit[i].length;j++){
      print(namesSplit[i][j]);  
    }
    
  }

Output: A mi r and A bbas Output:A mi r 和 A bbas

You can filter the runes and get all the letters from Az.您可以过滤符文并获取来自 Az 的所有字母。

List<String> singleWord = runes.where((rune) => rune > 64 && rune < 123).map((rune) => String.fromCharCode(rune)).toList()

print(singleWord); // prints [A, m, i, r, A, b, b, a, s]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM