【问题标题】:how to decompose an array into different strings如何将数组分解为不同的字符串
【发布时间】:2020-04-29 03:22:53
【问题描述】:

所以我将一个数组从一个活动传递到另一个活动, 当我将数组发送到其他活动时,我想将数组分解为各种字符串。 例如:

在第一个活动中:

array[] x = [car,ball,mouse,shirt]

在第二个活动中:

接收 x array[] ,然后将其分解为:

string one = "car"
string two = "ball"
string three = "mouse"
string four = "shirt"

例如,我可以对其中的每一个单独使用 Toast 操作。

这是我的实际代码:

头等舱:

Bundle b=new Bundle();
b.putStringArray("key",new String[]{repeat,temp,humidit,activer,food});
Intent i=new Intent(recruiter.this, StructureClass.class);
i.putExtras(b);

二等:

Bundle b = this.getIntent().getExtras();
final String[] array= b.getStringArray("key");



Toast.makeText(StructureClass.this, "i want the string to be shown here 
separately for each value! " , Toast.LENGTH_LONG).show();

【问题讨论】:

  • 把你的字符串改成这样new String[]{"repeat","temp","humidit","activer","food"};

标签: java android arrays string


【解决方案1】:

据我了解,您希望将数组拆分为带分隔符的字符串

List<String> list = Arrays.asList(x);
String.join(", ", list);

【讨论】:

  • @prozhan 最好在 StackOverflow 中将其标记为已回答
【解决方案2】:

我认为这段代码可以帮助你。

更新


    final String[] s= b.getStringArray("key");
    String a = String.join(", ", s);
    Toast.makeText(StructureClass.this, a , Toast.LENGTH_LONG).show();

【讨论】:

  • 这将在末尾添加一个额外的逗号。
  • @PrashantJha 你是测试中的佼佼者
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多