1

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

  1. Is possible to remove [] from the result?

  2. How can I progrmatically format names like this A.Jones, R.Flip & F.Fahim from result?

4

2 回答 2

7

您可以按照您描述的方式格式化名称,例如:

System.out.println("Alex Jones".replaceAll("^(\\w)\\w+", "$1."));
A.琼斯

至于你的第一个问题,那应该只是一个简单的substring调用:arrayString.substring(1, arrayString.length() - 1).

于 2013-07-16T17:02:46.697 回答
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]));

}
于 2013-07-16T17:03:17.800 回答