我有一个字符串,如下所示2 4 12 12 yellow Hi how are you我想像这样拆分字符串{2,2,12,12,yellow, Hi how are you},以便将列表中的项目作为构造函数中的参数传递。有什么帮助吗?
1 回答

互换的青春
TA贡献1797条经验 获得超6个赞
简单的答案是拆分字符串:
String[] fragments = theString.split(" ", 6);
鉴于片段具有特定含义(并且可能您希望其中一些作为非字符串类型),使用 a 可能更具可读性Scanner:
Scanner sc = new Scanner(theString);
int x = sc.nextInt();
int y = sc.nextInt();
int width = sc.nextInt();
int height = sc.nextInt();
String color = sc.next();
String message = sc.nextLine();
如果您从文件或标准输入中读取这些字符串,这种方法也更容易:只需Scanner在 FileReader/InputStream 上创建。
添加回答
举报
0/150
提交
取消