1

Is there a way of leaving a space where it is necessary when invoking getClass().getSimpleName().toLowerCase() method?

For instance, I have several different classes whose names are PublicTransportation, CityBus, Metro, Tram, Ferry, Aircraft.

I have defined my toString() methods but would like to leave a space between Public and Transportation. Same thing for the CityBus class. Here are my toString() methods:

//PublicTransportation class
public String toString()
{
    return "This " + getClass().getSimpleName().toLowerCase() + " costs " + getTicketPrice() + ". It has " + getNumOfStops() + " number of stops.";
}

//CityBus class
public String toString()
{
    return super.toString() + " It's route number is " + getRouteNum()
            + ".\nIt's operation start year is " + getBeginOpYear() + ". The line name is " + getLineName() + ". The driver is " 
            + getDriverName() + ".";
}
Grzegorz Górkiewicz
  • 4,496
  • 4
  • 22
  • 38
JKawa
  • 179
  • 2
  • 6
  • 20
  • i think your question is not clear can you explain more with some examples? – Youcef LAIDANI Feb 14 '17 at 21:11
  • `getSimpleName` is not related to `toString` in any way: `toString` is a method that might return different results for different instances while `getSimpleName` is a class method! – Nir Alfasi Feb 14 '17 at 21:11
  • 1
    You could save the `getClass().getSimpleName().toLowerCase()` result in a variable and then add the space as wanted. – Lopan Feb 14 '17 at 21:11
  • Possible duplicate of [How do I convert CamelCase into human-readable names in Java?](http://stackoverflow.com/questions/2559759/how-do-i-convert-camelcase-into-human-readable-names-in-java) – ahmed.seddiq Feb 14 '17 at 21:18

3 Answers3

4

One way to do it would be a regex. For instance, System.out.println("CityBus".replaceAll("([a-z])([A-Z])", "$1 $2")) will print out City Bus (with a space). So in your case, toString() would be something like this:

public String toString()
{
    String expandedName = getClass().getSimpleName().replaceAll("([a-z])([A-Z])", "$1 $2");
    return "This " + expandedName + " costs " + getTicketPrice() + ". It has " + getNumOfStops() + " number of stops.";
}

It can be made a little better by making the expandedName string static so we don't do the regex replacement on each call to toString(), but just once per class.

Edit:

replaceAll() takes two regular expressions, and replaces one with the other. The regex being replaced is ([a-z])([A-Z]), which translates to "a lower case letter, followed by an upper case letter" both letters are in parenthesis, which makes them capture groups. I then replace it with $1 $2 which means "capture group 1, space, capture group 2".

Malt
  • 28,965
  • 9
  • 65
  • 105
1

I think you can't do it in this way, because you get only class name and make it to lower case.

If possible, you can try to define you own getSimpleName method (or methods) returns class name in your wished format.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
0

You could split the class name by camel case using regex with:

getClass().getSimpleName().split("(?<=[a-z])(?=[A-Z])")

Then use a StringBuffer to construct the final string from the resulting array.

mwstc013
  • 86
  • 3