Home > OS >  How can I convert a List<String> to String []?
How can I convert a List<String> to String []?

Time:01-11

I have this String

List<String> params = ['A','B','C'];

I want to convert this to "['A']['B']['C']" How can I convert this properly?

CodePudding user response:

You can try:

void main(){
  List<String> params = ['A','B','C'];
  final out = params.map((e) => "['$e']").join();
  print(out);
}

Prints:

['A']['B']['C']

CodePudding user response:

you can do this

  List<String> params = ['A','B','C'];
  List newParams = [];
  for(var item in params){
    newParams.add([item]);
  }
  String stringParams = newParams.toString();
  String noBracketParams = stringParams.substring( 1, stringParams.length - 1 );
  String noCommasParams = noBracketParams.replaceAll(',', '');
  print(noCommasParams);

CodePudding user response:

  • I'm not sure what you're trying to do, but it can be achieved like this
    List<String> params = ['A', 'B', 'C'];
    List.generate(
        params.length, (index) => params[index] = '''['${params[index]}']''');
    var str = '';
    params.forEach((item) => str  = item);
    print(str);
  • Related