Baderous Posted June 25, 2008 Report Share Posted June 25, 2008 Preciso de tratar uma string, isto é, colocar o seu primeiro caracter em maiúsculas e os restantes em minúsculas. Como devo fazer? Eu consigo fazer o toLowerCase(), e sei que posso aceder ao 1º caracter através de charAt(index), mas não estou a conseguir fazer. Há algum método da classe StringBuilder que coloque um char em maiúsculas? Link to comment Share on other sites More sharing options...
Betovsky Posted June 25, 2008 Report Share Posted June 25, 2008 Estás a tentar converter o char na própria String? É que não podes fazer isso. As Strings são imutáveis, portanto o que tens de fazer é criar uma nova String em que vais pondo os chars já convertidos. "Give a man a fish and he will eat for a day; Teach a man to fish and he will eat for a lifetime. The moral? READ THE MANUAL !" Sign on a computer system consultant's desk Link to comment Share on other sites More sharing options...
Baderous Posted June 25, 2008 Author Report Share Posted June 25, 2008 Já consegui: public static String trataString(String f) { StringBuilder aux = new StringBuilder(f.toLowerCase()); char c = f.charAt(0); if (c>='a' && c<='z') c = (char)('A'+c-'a'); aux.setCharAt(0,c); return aux.toString(); } Agora já está. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now