sde Posted November 19, 2003 Posted November 19, 2003 what is the best way to make the first letter of a string upper case? Quote codenewbie
hog Posted November 19, 2003 Posted November 19, 2003 Messy, but hey if it works:) Dim strMyName As String = "paul" Dim strFirstLetter As String = Microsoft.VisualBasic.UCase(Microsoft.VisualBasic.Left(strMyName, 1)) strMyName = strFirstLetter & Microsoft.VisualBasic.Mid(strMyName, 2, Microsoft.VisualBasic.Len(strMyName) - 1) MessageBox.Show(strMyName) Quote My website
*Experts* mutant Posted November 19, 2003 *Experts* Posted November 19, 2003 (edited) Here would a simple code that would do it using .NET only (using compatibility functions is not very good): 'create a new string builder Dim sb As New System.Text.StringBuilder("some text") 'replace the first letter with its uppercase counterpart sb = sb.Replace(sb.Chars(0), Char.ToUpper(sb.Chars(0)), 0, 1) MessageBox.Show(sb.ToString) Edited November 19, 2003 by mutant Quote
sde Posted November 19, 2003 Author Posted November 19, 2003 thanks hog, .. do you know if i have to import something to use it in c#? i can't get it to work. Quote codenewbie
*Experts* Volte Posted November 19, 2003 *Experts* Posted November 19, 2003 Paste your C# code and any errors it is giving. Quote
sde Posted November 19, 2003 Author Posted November 19, 2003 i missed mutant's post before .. this code works: private string UCFirst(string strName) { System.Text.StringBuilder sb = new System.Text.StringBuilder(strName); sb = sb.Replace(sb[0], Char.ToUpper(sb[0]), 0, 1); return sb.ToString(); } thanks! Quote codenewbie
hog Posted November 20, 2003 Posted November 20, 2003 As my post stated, it was messy and yes I agree with mutant re using .net features is preferred! But it was late, I knew how to do it the old way but now I now the new way so everyones a winner :) Quote My website
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.