The Mobile Galaxy The New Mobile Era Has Come | Mobile – A Time To Remember

15May/120

How to change uppercase sentences to mixed text in Java?

/**
 *
 */
package com.go2log.kevin;
import java.text.BreakIterator;
import java.util.Locale;
/**
 * @author Kevin Sun
 *
 */
public class FormatParagraph {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String paragraph =
                "LINE BOUNDARY ANALYSIS DETERMINES WHERE A TEXT " +
                "STRING CAN BE BROKEN WHEN LINE-WRAPPING. THE " +
                "MECHANISM CORRECTLY HANDLES PUNCTUATION AND " +
                "HYPHENATED WORDS. ACTUAL LINE BREAKING NEEDS TO " +
                "ALSO CONSIDER THE AVAILABLE LINE WIDTH AND IS " +
                "HANDLED BY HIGHER-LEVEL SOFTWARE. ";

        BreakIterator iterator =
                BreakIterator.getSentenceInstance();

        String sentences = paragraphformatter(iterator, paragraph.toUpperCase());
        System.out.println("New Sentences: " + sentences);
    }

    private static String paragraphformatter(BreakIterator bi, String source) {
        StringBuffer formattedString = new StringBuffer();
        bi.setText(source);

        int lastIndex = bi.first();
        while (lastIndex != BreakIterator.DONE) {
            int firstIndex = lastIndex;
            lastIndex = bi.next();

            if (lastIndex != BreakIterator.DONE) {
                String sentence = source.substring(firstIndex, lastIndex);
                System.out.println("sentence = " + sentence);
                formattedString.append(toProperCase(sentence.toLowerCase()));

            }
        }
        return formattedString.toString();
    }

    private static String toProperCase(String name) {
        return name.substring(0, 1).toUpperCase() +
                   name.substring(1).toLowerCase();
    }
}

Result:
sentence = LINE BOUNDARY ANALYSIS DETERMINES WHERE A TEXT STRING CAN BE BROKEN WHEN LINE-WRAPPING.
sentence = THE MECHANISM CORRECTLY HANDLES PUNCTUATION AND HYPHENATED WORDS.
sentence = ACTUAL LINE BREAKING NEEDS TO ALSO CONSIDER THE AVAILABLE LINE WIDTH AND IS HANDLED BY HIGHER-LEVEL SOFTWARE.
New Sentences: Line boundary analysis determines where a text string can be broken when line-wrapping. The mechanism correctly handles punctuation and hyphenated words. Actual line breaking needs to also consider the available line width and is handled by higher-level software.
Share
Tagged as: , No Comments