Question: I'm trying take the names that I am given by the program after it retrieves them from unstructured text, and display them in a user specified Format of either "First MI. Last" or "Last, First MI". Any ideas? So far it checks to see if a comma is present in the string. if so I want to switch the order of the words in the string and remove the comma, and if there is a middle initial and it doesnt' contain a period after it, I want to add one.

```
if (entity instanceof Entity) {
    // if so, cast it to a variable
    Entity ent = (Entity) entity;

    SName name = ent.getName();
    String nameStr = name.getString();
    String newName = "";

    // Now you have the name to mess with
    // NOW, this is where i need help
    if (choiceStr.equals("First MI. Last")) {
        String formattedName = WordUtils
                .capitalizeFully(nameStr);
        for (int i = 0; i < formattedName.length(); i++) {

            if (formattedName.charAt(i) != ',') {
                newName += formattedName.charAt(i);
            }
        }
    }
    name.setString(newName);
    network.updateConcept(ent);

```

Answer: 
Use a regular expression and `String.replaceAll`:

```
"Obama, Barack H.".replace("(\\w+), (\\w+) (\\w\\.)", "$2 $3 $1")

```

The result is `Barack H. Obama`.
This would be easier with `substring`. This assumes that the format is valid (you'll have to check that).

```
//Separate the names
String newName;
String lastName = name.substring(0, name.indexOf(","));
String firstName = name.substring(name.indexOf(",")+1);

//Check for a space indicating a middle Name
//Check to see if the middle name already has the period if not add it
if(firstName.trim().contains(" ") && !firstName.contains(".")) {
   firstName += ".";
}

newName = firstName + " " + lastName;

//Set the name to whatever you're using

```

Note this won't work if names are allowed to contain `"," " " or "."`