Example:
I have a string "stack exchange"
, how do I transform its first character, getting "Stack exchange"
?
Example:
I have a string "stack exchange"
, how do I transform its first character, getting "Stack exchange"
?
This is it:
var texto = "stack exchange";
var upper = char.ToUpper(texto[0]) + texto.Substring(1);
See working on dotNetFiddle .
There are better ways to resolve the issue but that are out of the question requirement. If you are interested you already have a ask about it here .
This form is more readable to most programmers than using LINQ. It is obvious to anyone what the algorithm does. You do not need to add an extra namespace to run it. Legibility is subjective but size is not and this is shorter. It uses fewer methods. Of course it can be argued that it also uses 2 operators and that in the background they are methods, but C # is not Java. If operators are methods, it is implementation detail and one can not argue about expressiveness by ignoring this. I find it more expressive to use operators than methods. You have to find the opposite, So this is subjective. Each one chooses what is best for them.
Besides this, it is by chance, faster. I always argue in my responses that one should not do premature optimization, that one should not pursue worthless performance gains. But what is useful or not is not always obvious. If you go by this algorithm in a method that has potential to be used within long loops, the gain happens to be important. Otherwise, BCL methods would not have to be as optimized as they are. I'm not even preaching optimization for optimization, but if you have a simple way to do it like this, if it has no real drawback (being idiomatic is questionable, even because no one said that programming declaratively or functionally is right in C #, it's only an option, besides being idiomatic has to bring clear advantages, when this does not happen, it is silly to pursue ideals, it would be premature). In some scenarios the difference is not negligible .
Tip of SO in English , small method to do this:
public static string FirstCharToUpper(string input)
{
if (String.IsNullOrEmpty(input))
throw new ArgumentException("Insira uma palavra diferente de nula ou vazia");
return input.First().ToString().ToUpper() + input.Substring(1);
}
You may also be interested in other transformation , the Title Case (also known as Capitalize ). It turns "stack exchange" into " S tack E xchange":
public string ToTitleCase(string str)
{
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str.ToLower());
}
You can enter the functionality of the previous responses into a static class and access it from anywhere.
Deployment:
public static class StringExtends
{
public static string FirstCharToUpper(this string input)
{
if (String.IsNullOrEmpty(input))
throw new ArgumentException("Insira uma palavra diferente de nula ou vazia");
return input.Length > 1 ? char.ToUpper(input[0]) + input.Substring(1) : input.ToUpper();
}
}
Usage:
var texto = "stack exchange";
var upper = texto.FirstCharToUpper();