Broad Network


Changing the Case of Characters in Perl String

Handling Strings in Perl – Part 5

Perl Course

Foreword: In this part of the series, I explain how to change the casing of characters in a string; this includes title and highlight case.

By: Chrysanthus Date Published: 23 Oct 2015

Introduction

This is part 5 of my series, Handling Strings in Perl. In this part of the series, I explain how to change the casing of characters in a string; this includes title and highlight case. You should have read the previous parts of the series before reaching here, as this is a continuation.

The \l Escape Sequence
The \l operator changes the next character in a string to lowercase. For example, assume that you have the string,

    "This is the Man in charge."

You can change ‘M’ in “man” to lowercase to have the string,

    "This is the man in charge."

as in the following code:

use strict;

    my $str = "This is \lMan in charge.";
    print $str;

Because of the \l before M, the output is,

    This is the man in charge.

The lcfirst Function
This function changes the first character of a string to lower case. So the code segment,

    my $str = "This phone text msg is 2 tell yu that I will not do yr thing again.";
    my $strl = lcfirst($str);
    print $strl;


will output,

    “this phone text msg is 2 tell yu that I will not do yr thing again.”.

Note how the function has been used in the code: all the string is the argument to the function.

The \u Operator
The \u operator changes the next character in a string to uppercase. For example, assume that you have the string,

    "The queen of england."

You can change ‘e’ in “England” to uppercase to have the string,

    "The queen of England."

as in the following code:

use strict;

    my $str = "The queen of \uengland.";
    print $str;

Because of the \u before the e, the output is,

    The queen of England.

The ucfirst Function
This function changes the first character of a string to uppercase. So the code segment,

    my $str = ucfirst("this is a sentence.");
    print $str;

will output,

    This is a sentence.

Note how the function has been used in the code: all the string is the argument to the function.

The \E Indicator
The \E indicator is an escape sequence that can be placed anywhere within a string. With the indicator present, an operator can act on all the characters from the beginning of the string to the indicator. So, the indicator indicates at which point in the string the operator should stop its changing action.

The \L Operator
This operator changes all characters from its point of embedding in the string to the indicator, \E if the indicator is present or to the end of the string if the indicator is absent, to lowercase. Any character in the stretch that was already in lowercase, remains in lowercase. Read and try the following code:

use strict;

    my $str = "this \LSENTENCE NEEDS \Ecorrection.";
    print $str;

Note that \L is placed in the string where you want the change to start. The output is:

    “this sentence needs correction.”

The lc Function
This function changes all the characters in a string to lowercase. The characters that were already in lowercase remain in lowercase. Try the following code:

use strict;
    my $str = lc("THIS IS HIS THING.");
    print $str;

The output is:

    this is his thing.

The \U operator
This operator changes all characters from its point of embedding in the string to the indicator, \E if the indicator is present or to the end of the string if the indicator is absent, to uppercase. Any character in the stretch that was already in uppercase, remains in uppercase. Read and try the following code:

use strict;

    my $str = "The countries: \Uusa, uk\E are important.";
    print $str;

Note that \L is placed in the string where you want the change to start. The output is:

    The countries: USA, UK are important.

The uc Function
This function changes all the characters in a string to uppercase. The characters that were already in uppercase remain in uppercase. Try the following code:

use strict;

    my $str = uc("usa, uno, unesco, ussr, uk");
    print $str;

The output is:

    USA, UNO, UNESCO, USSR, UK

Title Case
There is a difference between title case and highlight case. To change a phrase to title case, means you uppercase the first letter of all the words in the phrase. The syntax is:

    $string =~ s/([\w']+)/\u\L$1/g

Now, ([\w']+) means a word, which can have the apostrophe, and it is captured. For the replacement, \L changes all the characters in the word to lowercase, and then \u changes the first character of the word to uppercase. Because of the g modifier, $1 is for all the captured strings. Try the following code:

use strict;

    my $str = "fred and barney's lodge";

    $str =~ s/([\w']+)/\u\L$1/g;

    print $str;

The output is:

    Fred And Barney's Lodge

That is good, but it may not be what you want. You may want highlight case – see below:

Highlight Case
Highlight case is like title case but prepositions, grammatical articles, conjunctions, pronouns, and words of quantity, are in lowercase. To achieve this, you first change to title case and then use arrays to replace all the non-keywords to lowercase. Then make sure the first character of the phrase is in uppercase. The following code illustrates this:

use strict;

    my $str = "fred and barney's lodge";

    $str =~ s/([\w']+)/\u\L$1/g;


    my @prepositions = ('about', 'along', 'among', 'before', 'after', 'by', 'for', 'in', 'from', 'on', 'of', 'since', 'to', 'until', 'till', 'up', 'with', 'between');

    my @grammaticalArticles = ('an', 'a', 'the');

    my @conjunctions = ('while', 'whereas', 'since', 'as', 'for', 'therefore', 'but', 'and', 'or');

    my @pronouns = ('I', 'you', 'he', 'she', 'we', 'they', 'me', 'him', 'her', 'us', 'them', 'my', 'your', 'his', 'her', 'our', 'their', 'mine', 'yours', 'hers', 'ours', 'theirs', 'it', 'its');

    my @wordsOfQuantity = ('some', 'few', 'many', 'much', 'little');


    $str=~s/$_/\L$_/ig foreach @prepositions;
    $str=~s/$_/\L$_/ig foreach @grammaticalArticles;
    $str=~s/$_/$_/ig foreach @conjunctions;
    $str=~s/$_/\L$_/ig foreach @pronouns;
    $str=~s/$_/\L$_/ig foreach @wordsOfQuantity;


    ucfirst($str);

    print $str;

The output is:

    Fred and Barney's Lodge

which is most likely what you want. Note the use of the i modifier for case insensitive search.

That is it for this part of the series. We stop here and continue in the next part.

Chrys

Related Links

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Advanced Course
Miscellaneous Features in Perl
Perl Two-Dimensional Structures
Advanced Perl Regular Expressions
Designing and Using a Perl Module
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course

BACK NEXT

Comments

Become the Writer's Fan
Send the Writer a Message