This is a simple question I guess, but I was trying to change just the first lower case letter of a line from a .txt file to an upper case, using the following
$_ =~ s/^[a-z]/\U/;
What happens, when I execute it, is that instead of changing the lower case to upper case the lower case at the beginning of the line is substituted with the most significant bit on the line. For example, the line nAkld987aBALPaapofikU88
instead of being substituted with NAkld987
becomes Akld987...
You can use str. title() to get a title cased version of the string. This converts the first character of each word in the string to uppercase and the remaining characters to lowercase.
Perl | uc() Function uc() function in Perl returns the string passed to it after converting it into uppercase. It is similar to ucfirst() function which returns only first character in uppercase.
The Perl lc() function takes a string, makes the entire thing lowercase and then returns the new string. For example: #!/usr/bin/perl.
You could/should use ucfirst
. I say should as it's much more obvious to a maintainer that your intent is to uppercase the first letter of the string. I love a regex, but in this case I feel it's not the correct approach.
my $str = "test"; print ucfirst($str);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With