First Letter to Upper Case

How can I capitalize the first letter of each word in a string using JavaScript?

You are not assigning your changes to the array again, so all your efforts are in vain. Try this:



function titleCase(str) {

var splitStr = str.toLowerCase().split(' ');

for (var i = 0; i < splitStr.length; i++) {

// You do not need to check if i is larger than splitStr length, as your for does that for you

// Assign it back to the array

splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);

}

// Directly return the joined string

return splitStr.join(' ');

}

document.write(titleCase("I'm a little tea pot"));

Uppercase first letter of variable

Use the .replace function to replace the lowercase letters that begin a word with the capital letter.



var str = "hello, world!";
str = str.toLowerCase().replace(/\b[a-z]/g, function(letter) {
return letter.toUpperCase();
});
alert(str); //Displays "Hello, World!"

Make first letter of a string upper case (with maximum performance)

Solution in different C# versions

C# 8 with at least .NET Core 3.0 or .NET Standard 2.1

public static class StringExtensions
{
public static string FirstCharToUpper(this string input) =>
input switch
{
null => throw new ArgumentNullException(nameof(input)),
"" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
_ => string.Concat(input[0].ToString().ToUpper(), input.AsSpan(1))
};
}

Since .NET Core 3.0 / .NET Standard 2.1 String.Concat() supports ReadonlySpan<char> which saves one allocation if we use .AsSpan(1) instead of .Substring(1).

C# 8

public static class StringExtensions
{
public static string FirstCharToUpper(this string input) =>
input switch
{
null => throw new ArgumentNullException(nameof(input)),
"" => throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input)),
_ => input[0].ToString().ToUpper() + input.Substring(1)
};
}

C# 7

public static class StringExtensions
{
public static string FirstCharToUpper(this string input)
{
switch (input)
{
case null: throw new ArgumentNullException(nameof(input));
case "": throw new ArgumentException($"{nameof(input)} cannot be empty", nameof(input));
default: return input[0].ToString().ToUpper() + input.Substring(1);
}
}
}

Really old answers

public static string FirstCharToUpper(string input)
{
if (String.IsNullOrEmpty(input))
throw new ArgumentException("ARGH!");
return input.First().ToString().ToUpper() + String.Join("", input.Skip(1));
}

This version is shorter. For a faster solution, take a look at Diego's answer.

public static string FirstCharToUpper(string input)
{
if (String.IsNullOrEmpty(input))
throw new ArgumentException("ARGH!");
return input.First().ToString().ToUpper() + input.Substring(1);
}

Probably the fastest solution is Darren's (There's even a benchmark) although I would change it's string.IsNullOrEmpty(s) validation to throw an exception since the original requirement expects for a first letter to exist so it can be uppercased. Note that this code works for a generic string and not particularly on valid values from the Textbox.

How to uppercase first letter (without changing the others) in Snowflake

another option is

UPPER(LEFT(my_string,1))||SUBSTR(my_string,2)

and if we use the same whitespace pattern, would win the code golf by 1:

INSERT(my_string,1,1,UPPER(LEFT(my_string,1)))

convert a string such that the first letter is uppercase and everythingelse is lower case

Just use str.title():

In [73]: a, b = "italic","ITALIC"

In [74]: a.title(), b.title()
Out[74]: ('Italic', 'Italic')

help() on str.title():

S.title() -> string

Return a titlecased version of S, i.e. words start with uppercase
characters, all remaining cased characters have lowercase.

uppercase first character in a variable with bash

foo="$(tr '[:lower:]' '[:upper:]' <<< ${foo:0:1})${foo:1}"

Make first letter of words uppercase in a string

There is a function in the built-in strings package called Title.

s := "INTEGRATED ENGINEERING 5 Year (BSC with a Year in Industry)"
fmt.Println(strings.Title(strings.ToLower(s)))

https://go.dev/play/p/THsIzD3ZCF9



Related Topics



Leave a reply



Submit