仅获取数组中的名字 [英] Getting only the first name in an array

查看:63
本文介绍了仅获取数组中的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的数组列表: ArrayList names = new ArrayList<>(); ,用于存储人们在不同教科书中输入的名字和姓氏。

I have an array list like this: ArrayList names = new ArrayList<>(); that stores people's first and last names when they are entered in different textbooks.

因此,当提示乔·拜登的提示是元素编号1时,巴拉克·奥巴马(Barack Obama)将是数组列表中的元素编号2。我的问题是,是否有可能仅从阵列中获得像Joe这样的名字而又没有得到Biden?

So when prompted to Joe Biden would be element number 1 then Barack Obama would be element number 2 in the array list. My question is that if it is possible to only get their first names like Joe from the array without getting Biden also?

推荐答案

是的,您可以做

names.get(0).split("\\s+")[0]  //this would get you "Joe"

可能会得到姓氏

names.get(0).split("\\s+")[1] //this would get you "Biden"

这种方式完全取决于您的名字和姓氏之间有一个空格。并显然将 0 编辑为所需的任何索引。

This way completely relies on the fact that you have a space in between their first and last name. and obviously edit the 0 to whatever index you would like.

这篇关于仅获取数组中的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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