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?

0

2 Answers 2

7

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).

Sign up to request clarification or add additional context in comments.

Comments

0

To get Alex Jones out of the array, use 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]));

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.