-2

I would like to convert a string to camelcase but avoid a word in that string.

public class camelTest
{
public static void main(String []args)
{
    String test = "St. KiTTs aND Nevis";
    System.out.println(toCamelCase(test));
}


    public static String toCamelCase(String test1)
    {
        String[] split = test1.split(" ");

        String ret = "";
        for (int i=0;i<split.length;i++)
        {
            ret=ret+split[i].substring(0,1).toUpperCase()+split[i].substring(1).toLowerCase()+" ";

        }
    return ret.trim();}
   }

The code above has the output of: St. Kitts And Nevis

I would like it instead to say: St. Kitts and Nevis

3 Answers3

1
for (int i=0;i<split.length;i++){
    if(split[i].equalsIgnoreCase("and")){
        ret = ret + split[i].toLowerCase() + " ";
    } else {
        ret=ret+split[i].substring(0,1).toUpperCase()+split[i].substring(1).toLowerCase()+" ";
    }
}
Simulant
  • 19,190
  • 8
  • 63
  • 98
0
public static String toCamelCase(String test1)
{
    String[] split = test1.split(" ");

    String ret = "";
    for (int i=0;i<split.length;i++)
    {
        if (split[i].equalsIgnoreCase("and")) {
            ret=ret+split[i].toLowerCase()+" ";
            continue;
        }
        ret=ret+split[i].substring(0,1).toUpperCase()+split[i].substring(1).toLowerCase()+" ";

    }
return ret.trim();
}
anirban
  • 674
  • 1
  • 7
  • 21
0

If you are previously know the word that does not need to start with capital letter, then you can check for these word before start capitalizing the first letter.

String someString = "and";
if(split[i].equalsignorecase(someString))

where somString could be any string that you do not want to capitalize its first letter.

Simulant
  • 19,190
  • 8
  • 63
  • 98
Salman
  • 1,236
  • 5
  • 30
  • 59