|
Proper Case Strings
Proper Case Strings - Missing from the string class is a function to convert a string to it's Proper Case. The function below skips words like 'and', 'or', 'if', 'the', 'of' and 'a' unless they appear at the start of the sentence. In addition, hypenated words are caped and the function also looks for names that start with 'Mc' or 'Mac'. The way the class is written, words in mixed case are skipped because they could be right as entered. In addition to the result shown by the CProperCase class and so that you can see the difference, the same string is also converted using the .Net TextInfo class.
Enter a string your convert to proper case.
The converted String will appear here.
using System;
using System.Text;
namespace Classes
{
/// <summary>
/// Convert a Sentence or a Word to Proper Case
/// </summary>
/// <usage>
/// CProperCase Pc = new CProperCase();
/// string result = Pc.properSentence("the cow jumped the moon.");
/// result returned as "The Cow Jumped Over the Moon."
/// or
/// string result = Pc.properSentence("very very-good");
/// result returned as "Very Very-Good"
/// </usage>
public class CProperCase
{
#region Constructor
/// <summary> Constructor</summary>
public CProperCase(){}
#endregion
#region Private Class Variables
/// <summary>Do Not Change these Words</summary>
private string[] SkipWords={"a","at","an","and","the","or","of","to"};
/// <summary>Look for Special Words like McDonald, MacDonald or O'Riely</summary>
private string[] SpecialWords={"Mac","Mc","O'","D'"};
/// <summary>Do Not Change these Words</summary>
private string[] NoChangeWords={"USA","NSAD"};
#endregion
#region Private Functions
/// <summary>
/// Looks for words that shouldn't be capitalized
/// They will be skipped if they don't start the sentence.
/// </summary>
/// <param name="theWord">Word to Check</param>
/// <returns><i>True</i>if a SkipWord</returns>
private bool isSkipWord(string theWord)
{ foreach (string Skip in SkipWords) if (theWord.Equals(Skip)) return true;
return false;
}
/// <summary>
/// Looks for Special Situations defined in SpecialWords
/// above like McDonald or MacDonald
/// </summary>
/// <param name="theWord">Word to Check</param>
/// <returns>Checked Word</returns>
private string CheckSpecialWords(string theWord)
{
foreach (string Sw in SpecialWords)
if (theWord.StartsWith(Sw) && theWord.Length>Sw.Length)
{ theWord = theWord.Substring(0,Sw.Length)
+ Char.ToUpper(theWord[Sw.Length])
+ (theWord.Length>Sw.Length+1?theWord.Substring(Sw.Length+1):"");
}
return theWord;
}
private string ChkHyphen(string theWord)
{
StringBuilder sb = new StringBuilder();
string[] words = theWord.Split('-');
int i = words.GetUpperBound(0);
if(i==0) return theWord;
foreach (string word in words)
{ sb.Append(properWord(word));
if(i>0) sb.Append("-");
--i;
}
return sb.ToString();
}
/// <summary>We never want a null string</summary>
/// <param name="word">word or sentence</param>
/// <returns>Checked word or Sentence</returns>
private string noNull(string theWord)
{
return theWord.Trim()+"";
}
#endregion
#region Convert Word to ProperCase
/// <summary>Return Converted Word</summary>
/// <param name="word">Word to Convert</param>
/// <returns>Converted Word</returns>
public string properWord(string theWord)
{
// Shouldn't be null, but check anyway
theWord = noNull(theWord);
// Skip zero length or any MIXED Case Words on the Assumption that
// Mixed Case Words could be right.
if(theWord.Length>0 && (theWord==theWord.ToUpper() || theWord==theWord.ToLower()))
{
// Start by Checking for Words that shouldn't be changed.
foreach (string Skip in NoChangeWords)
if(string.Compare(theWord,Skip,true)==0) return Skip;
// Handle Single Character Words that were no in Skip Section
if(theWord.Length==1) { theWord = theWord.ToUpper(); return theWord; }
// If Word not in List of Words not To Change, CAP first Character
theWord = theWord.Substring(0,1).ToUpper()+theWord.Substring(1).ToLower();
// Check for the SpecialWords
theWord = CheckSpecialWords(theWord);
// Check for Hyphenated Words
theWord = ChkHyphen(theWord);
}
return theWord;
}
#endregion
#region Convert Sentence to ProperCase
/// <summary>
/// Convert Sentence to ProperCase
/// </summary>
/// <param name="sentence">Sentence to Convert</param>
/// <returns>Proper Case Sentence</returns>
public string properSentence(string sentence)
{
sentence = noNull(sentence);
StringBuilder sb = new StringBuilder();
string[] words = sentence.Split(' ');
int i = 0;
foreach (string word in words)
{
// word is Readonly so use a copy of word
string theWord = word;
// Store any Ending Puncutation Characters
string EndChar = string.Empty;
if(theWord.Length>1 && Char.IsPunctuation(theWord[theWord.Length-1]))
{ EndChar = theWord.Substring(theWord.Length-1,1);
theWord = theWord.Substring(0,theWord.Length-1);
}
// Cap an Starting Word like 'a'
if(theWord.Length==1 && i==0) sb.Append(word.ToUpper());
else if(i>0 && isSkipWord(theWord)) sb.Append(theWord);
else sb.Append(properWord(theWord));
sb.Append(EndChar);
sb.Append(" "); // Add back the space
++i; // Increment after 1st word
}
return sb.ToString().Trim(); //Trim and Return
}
#endregion
}
}
|