拆分空格分隔列表 [英] Splitting a space separated list

查看:90
本文介绍了拆分空格分隔列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我面临的一项常见任务:将空格分隔列表拆分为 head 元素和包含 tail 元素的数组。例如,给定此字符串:

This is a common task I'm facing: splitting a space separated list into a head element and an array containing the tail elements. For example, given this string:

the quick brown fox

我们想要:

"the"
["quick","brown","fox"]

..在两个不同的变量中。第一个变量应该是一个字符串,第二个变量应该是一个数组。我正在寻找一种优雅方式(最好用Java)。

.. in two different variables. The first variable should be a string, and the second an array. I'm looking for an elegant way to do this (preferably in Java).

推荐答案

某些优雅的价值:

String input = "The quick brown fox";
String[] elements = input.split(" ");
String first = elements[0];
String[] trailing = Arrays.copyOfRange(elements,1,elements.length);

我想不出用更少的代码来做这件事的方法......

I can't think of a way to do it with less code...

这篇关于拆分空格分隔列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆