0

Can anyone please tell me why the following code prints 1 the high street and not 1 The High Street?:

    String propertyPageTitle = "1-the-high-street";
    propertyPageTitle = propertyPageTitle.replace("-", " ");
    WordUtils.capitalizeFully(propertyPageTitle);
    System.out.println(propertyPageTitle);

EDIT to show solution:

    String propertyPageTitle = "1-the-high-street";
    propertyPageTitle = propertyPageTitle.replace("-", " ");
    propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);
    System.out.println(propertyPageTitle);

Supposing I wanted to ignore the word 'and' if it appears (I'm reading values from a .csv) and NOT change to titlecase? how would that be possible.

Steerpike
  • 1,712
  • 6
  • 38
  • 71

4 Answers4

1

WordUtils.capitalizeFully does not change the original String, but instead returns the capitalized String.

propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • really, I want to capitalize fully (first letter of each String) UNLESS the word is 'and'. So I suppose I can build some logic around capitalizeFully to handle this scenario – Steerpike Feb 23 '14 at 11:17
1

This happens because capitalizeFully(String) of WordUtils returns a String which has the expected answer. So try:

propertyPageTitle = WordUtils.capitalizeFully(propertyPageTitle);

And then it will work.

Aman Agnihotri
  • 2,973
  • 1
  • 18
  • 22
0
String firstStr = "i am fine";
String capitalizedStr = WordUtils.capitalizeFully(firstStr);
System.out.println(capitalizedStr);

The return should be taken to get the output of a method. It is common for all methods in Java String

AJJ
  • 3,570
  • 7
  • 43
  • 76
0
    String toBeCapped = "1 the high street and 2 low street";

    String[] tokens = toBeCapped.split("\\s");
    StringBuilder builder = new StringBuilder();

    for (int i = 0; i < tokens.length; i++) {
        if (!tokens[i].equalsIgnoreCase("and")) {
            char capLetter = Character.toUpperCase(tokens[i].charAt(0));
            builder.append(" ");
            builder.append(capLetter);
            builder.append(tokens[i].substring(1, tokens[i].length()));
        } else {
            builder.append(" and");
        }
    }
    toBeCapped = builder.toString().trim();
    System.out.println(toBeCapped);

output:

1 The High Street and 2 Low Street
Melih Altıntaş
  • 2,495
  • 1
  • 22
  • 35