【问题标题】:Store String variable to String Array [duplicate]将字符串变量存储到字符串数组[重复]
【发布时间】:2020-09-20 01:35:14
【问题描述】:

这就像一个字符串数组 {hi, hi, hello},但是如果我有一个形状像这样的字符串,我可以通过在字符串数组中输入一个字符串变量来做到这一点吗?

像这样:
String animal = "袋鼠、兔子、狗、猫、大象";
String[] 数组 = { 动物 };

结果是:
数组[0] : 袋鼠
数组[1]:兔子

【问题讨论】:

标签: java arrays string


【解决方案1】:
   String animal = "kangaroo, rabbit, dog, cat, elephant";
   String[] array = animal.split(", ");
   System.out.println(Arrays.toString(array));

【讨论】:

  • 使用\\s*,\\s* 而不是", ",因为逗号后面可能有也可能没有空格。
猜你喜欢
  • 1970-01-01
  • 2018-12-01
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 2013-08-04
  • 2012-12-15
  • 1970-01-01
相关资源
最近更新 更多