I am using Regex.Replace to replace anything other A-Z characters. That works perfectly. Now I am just curious as to how do I make it so no matter what the First letter of the string is capitalized and the rest is lower case? I have a textfield with a maximum of 14 characters. Thanks in advance!
Call this and you will get the first letter of the string in upper case.
string UppercaseFirst(string s)
{
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
return char.ToUpper(s[0]) + s.Substring(1);
}
If you want to make it faster performance wise use the method below…
string UppercaseFirst(string s){
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
I do not know the exact syntax for what must be done, but this should be the logic. You can try to select the first letter and then convert it. Look it up to see how it should be done.
am just adding my solution since the others didn’t quite work for what I needed.
this capitalises the first letter in the sentence
private string UpperFirst(string text)
{
return char.ToUpper(text[0]) +
((text.Length > 1) ? text.Substring(1).ToLower() : string.Empty);
}
I was wondering about the same for about a couple of last days. Was trying different methods but none of those were working. Right now, I’m conducting some research for EduBirdie (Paraphrasing Tool Online | #1 Free Essay Rephraser - EduBirdie.com) about different techniques in writing in various programmes, and I was curious about the question that was asked above.
Hey there, I came up with this quickly it may or may not be just what you’re after…
public static string TidyCase(string sourceStr)
{
sourceStr.Trim();
if (!string.IsNullOrEmpty(sourceStr))
{
char[] allCharacters = sourceStr.ToCharArray();
for (int i = 0; i < allCharacters.Length; i++)
{
char character = allCharacters*;*
if (i == 0)
{
if (char.IsLower(character))
{
allCharacters = char.ToUpper(character);
}
}
else
{
if (char.IsUpper(character))
{
allCharacters = char.ToLower(character);
}
}
}
return new string(allCharacters);
}
return sourceStr;
}
Hope this helps,
Phill