Breaking Strings into chars that are in upper case

后端 未结 5 425
温柔的废话
温柔的废话 2021-01-14 18:46

I\'m making a method to read a whole class code and do some stuff with it.

What I want to do is get the name of the method, and make a String with it.

Someth

5条回答
  •  星月不相逢
    2021-01-14 19:00

    @MrWiggles is right. Just one more way to do this without being fancy :)

    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,
                    "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z", 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);
        }
    }
    

提交回复
热议问题