[英]How can I work with Java string arrays?
I have a String Array. 我有一个字符串数组。 Whenever I fetch an Array, I get data like this
每当我获取一个数组时,我都会得到这样的数据
[Alex Jones, Robert Flip, Farabi Fahim]
Now, my question is 现在,我的问题是
Is possible to remove []
from the result? 是否可以从结果中删除
[]
?
How can I progrmatically format names like this A.Jones, R.Flip & F.Fahim
from result? 如何从结果中以编程方式格式化诸如
A.Jones, R.Flip & F.Fahim
名称?
You can format the names in the way you describe with something like: 您可以使用以下描述的方式格式化名称:
System.out.println("Alex Jones".replaceAll("^(\\w)\\w+", "$1."));
A. Jones
As for your first question, that should just be a simple substring
call: arrayString.substring(1, arrayString.length() - 1)
. 至于第一个问题,那应该只是一个简单的
substring
调用: arrayString.substring(1, arrayString.length() - 1)
。
To get Alex Jones out of the array, use names[0]
. 要使Alex Jones脱离数组,请使用
names[0]
。
private String fixName(String name)
{
//break into name and last name
int whiteIndex = name.indexOf(' ');
String newName = name.substring(0, 1) + "." + name.substring(whiteIndex, name.length);
return newName;
}
public static void main()
{
String[] names = whateverYouUseToGetNames();
System.out.println(Arrays.toString(names));
for(int i = 0; i < names.length; i++)
System.out.println(fixName(names[i]));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.