Overview#

Convert A String To Title Case: The First Letter Of Each Word Is Converted To Upper Case Like This. Bonus: Convert a string to sentence case.

Does some correction of Name Anomalies

The Source Code:

import java.util.*;

public class convCase { 

	//**************************************
	// Name: Convert A String To Title Case
	// Description:Convert A String To Title Case: The First Letter Of Each Word Is Converted To Upper Case Like This. Bonus: Convert a string to sentance case like this.
	// By: Martin Budden
	//
	//
	// Inputs:String
	//
	// Returns:String
	//
	//Assumes:None
	//
	//Side Effects:None
	//This code is copyrighted and has limited warranties.
	//Please see http://www.Planet-Source-Code.com/xq/ASP/txtCodeId.3176/lngWId.2/qx/vb/scripts/ShowCode.htm
	//for details.
	//
	// Modified (extensively?) by John Johnson
	// 12/26/2002 -- Converts source string to lower case first to enforce Proper Case
	// 12/27/2002 -- Added handling for special names like O'Reilly, McDonald, Jones-Mardis
	//
	//**************************************
	
	// Convert A String To Title Case:
	public String toTitleCase( String inStr )
	
	
	    {
	    // Convert source string to lower case first
	    inStr = inStr.toLowerCase();

	    // Build a list of all the letters in the string:
	    List letterList = new ArrayList();
	
	
	        for ( int i = 0; i < inStr.length(); i++ ) {
	        letterList.add(inStr.substring(i,(i+1)));
	    }
	    // Build a new string letter by letter in outStr:
	    String outStr = "";
	    String strThisLetter = "";
	    String strPreviousLetter = " ";
	    String strPrevious2Letters = "  ";
	    // Iterate through the list one letter at a time:
	    Iterator iter = letterList.iterator();
	
	
	        while ( iter.hasNext() ) {
	        strThisLetter = ( String ) iter.next();

			if ( strPreviousLetter.equals(" ") || strPreviousLetter.equals("-") || strPreviousLetter.equals("'") ) {
				strThisLetter = strThisLetter.toUpperCase();
				outStr += strThisLetter;
			} else {
				// Handle mixed case names:
				if ( strPrevious2Letters.equals("Mc") ) {
					//System.out.println("TRUE strThisLetter='"+strThisLetter+"'"+"strPrevious2Letters='"+strPrevious2Letters+"'");
					outStr += strThisLetter.toUpperCase();
				} else {
					//System.out.println("FALSE strThisLetter='"+strThisLetter+"'"+"strPrevious2Letters='"+strPrevious2Letters+"'");
					// No matches, leave this letter alone:
					outStr += strThisLetter;
				}
			}

	        strPrevious2Letters = strPreviousLetter+strThisLetter;
	        strPreviousLetter = strThisLetter;
	    }

	    // Handle harder names (e.g. MacGregor vs. Macek) via brute force
	    // This is hardly all encompassing, but until I find a better way, this will
	    //   have to do.  This is an unweildy implementation, probably bad for performance.
	    //   This really should change ASAP, or be left out entirely.
	    if ( outStr.indexOf("Mac") > -1 ) {
		    outStr = replace(outStr,"Macarthur","MacArthur");
		    outStr = replace(outStr,"Macaulay","MacAulay");
		    outStr = replace(outStr,"Maccari","MacCari");
		    outStr = replace(outStr,"Maccaskill","MacCaskill");
		    outStr = replace(outStr,"Macdonald","MacDonald");
		    outStr = replace(outStr,"Macfarlane","MacFarlane");
		    outStr = replace(outStr,"Macgill","MacGill");
		    outStr = replace(outStr,"Macgilvray","MacGilvray");
		    outStr = replace(outStr,"Macgregor","MacGregor");
		    outStr = replace(outStr,"Maclean","MacLean");
		    outStr = replace(outStr,"Macleod","MacLeod");
		    outStr = replace(outStr,"Macmillan","MacMillan");
		    outStr = replace(outStr,"Macneel","MacNeel");
		    outStr = replace(outStr,"Macneil","MacNeil");
		    outStr = replace(outStr,"Macnicol","MacNicol");
		    outStr = replace(outStr,"Macpherson","MacPherson");
		    outStr = replace(outStr,"Macrory","MacRory");
	    }


	    return outStr;
	}
	// Free bonus!!!
	// Convert a string to sentence case:
	public String toSentenceCase( String inStr )


	    {
	    String outStr;
	    outStr = inStr.substring(0,1).toUpperCase();
	    outStr += inStr.substring(1);
	    return outStr;
	}

    // Taken from http://www.javaalmanac.com/egs/java.lang/ReplaceString.html
    static String replace(String str, String pattern, String replace) {
        int s = 0;
        int e = 0;
        StringBuffer result = new StringBuffer();
    
        while ((e = str.indexOf(pattern, s)) >= 0) {
            result.append(str.substring(s, e));
            result.append(replace);
            s = e+pattern.length();
        }
        result.append(str.substring(s));
        return result.toString();
    }

}

More Information#

There might be more information for this subject on one of the following:

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-5) was last changed on 04-Jul-2016 12:32 by jim