English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

What is the algorithm if I want to make a single character in a string to be a capital letter?? Lets say I have a string "What" and I want it to be "Atwhay". If the original words starts with a capital then I want the transformed word to be starting with a capital word too. How do I do this?

2006-10-30 03:07:58 · 2 answers · asked by ? 1 in Computers & Internet Programming & Design

2 answers

After modifying your string, use the String method toLowerCase():

String modified = ...;
modified = modified.toLowerCase();

then change the first character to uppercase:

char[] characters = modified.toCharArray();
characters[0] = Character.toUpperCase(characters[0]);

String result = new String(characters);

2006-10-30 09:32:52 · answer #1 · answered by vincentgl 5 · 0 0

I would not reinvent the wheel, there are many, many utility methods in the jakarta commons lang project. Here is the class called StringUtils and it has a method that you may be interested in called capatalize.

http://jakarta.apache.org/commons/lang/api-release/org/apache/commons/lang/StringUtils.html

If you really need to know how the method capatalize works you can download the open source for this class and look at the method.

I always use jakarta code because it is tested and maintained by the best of the java community.

http://jakarta.apache.org/site/downloads/downloads_commons-lang.cgi

2006-10-31 23:46:43 · answer #2 · answered by CM 2 · 0 0

fedest.com, questions and answers