Creates a mixed mode string out of a string. - Java java.lang

Java examples for java.lang:String Tokenize

Description

Creates a mixed mode string out of a string.

Demo Code

/*/*from  w  w  w  . j a va 2s .  c  o m*/
  You may freely copy, distribute, modify and use this class as long
  as the original author attribution remains intact.  See message
  below.

  Copyright (C) 2004 Christian Pesch. All Rights Reserved.
 */

//package com.java2s;

import java.util.StringTokenizer;

public class Main {
    public static void main(String[] argv) {
        String string = "java2s.com";
        System.out.println(toMixedCase(string));
    }

    /**
     * Creates a mixed mode string out of a string. Each space separated
     * substring will have an uppercase first letter and a lowercase rest.
     *
     * @param string the string to create a mixed mode string from
     * @return a mixed mode string out of a string. Each space separated
     *         substring will have an uppercase first letter and a lowercase rest
     */
    public static String toMixedCase(String string) {
        StringBuilder builder = new StringBuilder();
        StringTokenizer tokenizer = new StringTokenizer(string, "_ ", true);
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            if (token.length() > 1)
                builder.append(token.substring(0, 1).toUpperCase()).append(
                        token.substring(1).toLowerCase());
            else
                builder.append(token);
        }
        return builder.toString();
    }
}

Related Tutorials