Reformat a sentence by converting the first character as lower case and removing the last period. - Java java.lang

Java examples for java.lang:String Case

Introduction

The following code shows how to Reformat a sentence by converting the first character as lower case and removing the last period.

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String sentence = "this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. this is a test. ";
        System.out.println(stripSentence(sentence));
    }// w  ww.ja v  a2 s.  c  o  m

    /**
     * <p>Reformat a sentence by converting the first character as lower
     * case and removing the last period.</p>
     * @param sentence original sentence
     * @return updated sentence.
     */
    public static String stripSentence(final String sentence) {
        if (sentence == null) {
            throw new IllegalArgumentException(
                    "Cannot strip an undefined sentence");
        }

        char[] charsSeq = sentence.toCharArray();
        int numChars = charsSeq.length;
        char lastChar = charsSeq[charsSeq.length - 1];

        /*
         * Remove the last trailing character for the sentence in 
         * order to extract the correct stem from the last word
         * or N-Gram in the sentence.
         */
        if (lastChar == '.' || lastChar == '?' || lastChar == '!') {
            numChars--;
        }

        return String.valueOf(charsSeq, 0, numChars);
    }
}

Related Tutorials