Split strings into uppercase characters

I am making a method for reading whole class code and doing some things with it.

What I want to do is get the name of the method and create a string with it.

Something like removeProduct

I will create the line "Delete product"

How can I split the name method in case of investment? How can I build this new line with the first letter of each word as capital? I am doing this with a substring, is there an easier and better way to do this?

ps: I'm sure my Brazilian English did not help by name. If anyone can do it better, I will be grateful.

+3
java string
Dec 21 '10 at 17:45
source share
5 answers

You can use the regular expression to split the name into different words, and then capital letters:

public static void main(String[] args) { String input = "removeProduct"; //split into words String[] words = input.split("(?=[AZ])"); words[0] = capitalizeFirstLetter(words[0]); //join StringBuilder builder = new StringBuilder(); for ( String s : words ) { builder.append(s).append(" "); } System.out.println(builder.toString()); } private static String capitalizeFirstLetter(String in) { return in.substring(0, 1).toUpperCase() + in.substring(1); } 

Please note that this requires more convenient corner processing, for example, not adding a space to the end and processing 1-char words.

Change I wanted to explain the regex. A regular expression (?=[AZ]) is a zero-width statement (positive expression) corresponding to the position where the next character is between 'A' and 'Z.

+4
Dec 21 '10 at 17:52
source share

Do not try to invent a wheel, use commons-lang method

 String input = "methodName"; String[] words = StringUtils.splitByCharacterTypeCamelCase(methodName); String humanised = StringUtils.join(words, ' '); 
+6
Dec 21 '10 at 18:28
source share
 public String convertMethodName(String methodName) { StringBuilder sb = new StringBuilder().append(Character.toUpperCase(methodName.charAt(0))); for (int i = 1; i < methodName.length(); i++) { char c = methodName.charAt(i); if (Character.isUpperCase(c)) { sb.append(' '); } sb.append(c); } return sb.toString(); } 

Processing this method can give you finer control if you want to add functionality later for other situations (several headers in a line, etc.). In principle, for each character, it simply checks whether it is in capital letters (character codes 65-90, inclusive), and if so, adds a space to the buffer before the word begins.

EDIT: Using Character.isUpperCase ()

0
Dec 21 '10 at 18:01
source share

You can do this in 2 steps:

1 - Make the first letter of the string in uppercase.

2 - Insert a space before the uppercase letter, which is preceded by a lowercase letter.

For step 1 you can use the function, and for step 2 you can use the String.replaceAll method:

 String str = "removeProduct"; str = capitalizeFirst(str); str = str.replaceAll("(?<=[^AZ])([AZ])"," $1"); static String capitalizeFirst(String input) { return input.substring(0, 1).toUpperCase() + input.substring(1); } 

Code in action

0
Dec 21 '10 at 18:16
source share

@MrWiggles is right. Another way to do this without pretending :)

 import java.util.StringTokenizer; public class StringUtil { public static String captilizeFirstLetter(String token) { return Character.toUpperCase(token.charAt(0)) + token.substring(1); } public static String convert(String str) { final StringTokenizer st = new StringTokenizer(str, "ABCDEFGHIJKLMNOPQRSTU VWXYZ", true); final StringBuilder sb = new StringBuilder(); String token; if (st.hasMoreTokens()) { token = st.nextToken(); sb.append(StringUtil.captilizeFirstLetter(token) + " "); } while (st.hasMoreTokens()) { token = st.nextToken(); if (st.hasMoreTokens()) { token = token + st.nextToken(); } sb.append(StringUtil.captilizeFirstLetter(token) + " "); } return sb.toString().trim(); } public static void main(String[] args) throws Exception { String words = StringUtil.convert("helloWorldHowAreYou"); System.out.println(words); } } 
0
Dec 21 '10 at 18:55
source share