- Home /
Capitalize first letter in Textfield Only?
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!
Answer by meer59 · Nov 30, 2016 at 07:38 AM
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);
}
Thanks for this, I just used it. $$anonymous$$ade a small addition to it though, as I need the first letter capitalised, but the rest lower case.
string UppercaseFirst(string s){
if (string.IsNullOrEmpty(s))
{
return string.Empty;
}
input = input.ToLower();
char[] a = s.ToCharArray();
a[0] = char.ToUpper(a[0]);
return new string(a);
}
Answer by Phles · Oct 06, 2014 at 01:26 PM
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[i];
if (i == 0)
{
if (char.IsLower(character))
{
allCharacters[i] = char.ToUpper(character);
}
}
else
{
if (char.IsUpper(character))
{
allCharacters[i] = char.ToLower(character);
}
}
}
return new string(allCharacters);
}
return sourceStr;
}
Hope this helps, Phill
Answer by Yay_For_Humzah · Nov 30, 2016 at 07:38 AM
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.
Answer by NatCou · Feb 21, 2019 at 11:21 AM
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);
}
Answer by williamohare · Feb 21, 2019 at 12:37 PM
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 (https://edubirdie.com/paraphrasing-tool) about different techniques in writing in various programmes, and I was curious about the question that was asked above.
A different approach then all the answers this questions post; To make the first letter capitalized while the rest is lower: str = str.Substring(0,1).ToUpper() + str.Substring(1).ToLower();
Thank you!! ^.^ that worked for me. I had an if-else function that checked to make sure the sentence was valid before altering it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613164457im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
change font size help - impossible? 1 Answer
Monodevelopement not responding? 2 Answers
Money System 1 Answer
rigidbody2D.velocity.y always returns 0 2 Answers