You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何从单个字符串提取单词生成字符串ArrayList?

把空格分隔的字符串转换为字符串ArrayList的实现方法

Java 实现

直接通过字符串分割+数组转集合的方式完成转换:

import java.util.ArrayList;
import java.util.Arrays;

public class StringToListDemo {
    public static void main(String[] args) {
        String phrase = "educated washroom stinging breeze regroup earphone threaten epidemic hazy imbecile fritter bony";
        // 按空格分割字符串,再转换为可修改的ArrayList
        ArrayList<String> stringList = new ArrayList<>(Arrays.asList(phrase.split(" ")));
        // 验证结果
        System.out.println(stringList);
    }
}

注意:Arrays.asList()返回的是固定大小的List,若需要支持增删操作,必须传入ArrayList的构造器重新创建集合。

Python 实现

Python中没有专门的ArrayList类,内置的list即可满足需求,直接调用字符串的split()方法:

phrase = "educated washroom stinging breeze regroup earphone threaten epidemic hazy imbecile fritter bony"
# 默认按任意空白字符分割,返回字符串列表
stringList = phrase.split()
print(stringList)

split()不带参数时会自动处理连续空格、制表符等空白,比指定split(" ")更灵活。

内容的提问来源于stack exchange,提问作者wolfsoft Developer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 21:37:08