Beanie posted on February 25, 2009 09:47

This is a function to make a string into title case ie every word has a capital letter to start.  

In Classic ASP:

<%Function Tcase(strString)
    if instr(strString," ") then
        strStringArray=split(strString," ")
        for i=0 to ubound(strStringArray)
            if i=0 then
                strString=UCase(Left(strStringArray(i), 1)) & LCase(Mid(strStringArray(i), 2))
            else               
                strString=strString & " " & UCase(Left(strStringArray(i), 1)) & LCase(Mid(strStringArray(i), 2))
            end if
        next
        Tcase= strString
    else
        Tcase = UCase(Left(strString, 1)) & LCase(Mid(strString, 2))
    end if
end Function%>

In C# (C Sharp):

 

public string Tcase(string strString)
{
if (strString.IndexOf(" ",0)>0)
{
char[] delimchar = { ' ' };
string[] strStringArray = strString.Split(delimchar);
int i = 0;
for (i = 0; i <= strStringArray.Length-1; i++)
{
if (i == 0)
{
strString = Left(strStringArray[i], 1).ToUpper() +  Mid(strStringArray[i], 1).ToLower();
}
else
{
strString = strString + " " + Left(strStringArray[i], 1).ToUpper() +  Mid(strStringArray[i], 1).ToLower();
}
}
}
else
{
strString = Left(strString, 1).ToUpper() + Mid(strString, 1).ToLower();
}
return strString;
}
public static string Left(string param, int length)
{
string result = param.Substring(0, length);
return result;
}
public static string Right(string param, int length)
{
string result = param.Substring(param.Length - length, length);
return result;
}
public static string Mid(string param, int startIndex)
{
string result = param.Substring(startIndex);
return result;
} 

 

In ASP.NET (VB): 

  Public Function Tcase(ByVal strString As String) As String
    If strString.IndexOf(" ", 0) <> 0 Then
        Dim delimchar As Char = " "
        Dim strStringArray As String() = strString.Split(delimchar)
        Dim i As Integer = 0
        For i = 0 To strStringArray.Length
            If i = 0 Then
                strString = Left(strStringArray(i), 1).ToUpper() + Mid(strStringArray(i), 2).ToLower()
            Else
                strString = (strString & " ") + Left(strStringArray(i), 1).ToUpper() + Mid(strStringArray(i), 2).ToLower()
            End If
        Next
    Else
        strString = Left(strString, 1).ToUpper() + Mid(strString, 2).ToLower()
    End If
    Return strString
End Function


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Comments

Add comment


 
  Country flag

biuquote
  • Comment
  • Preview
Loading



Page List

Search Blog

Tag Cloud

Recent Comments

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Beanie