Lesson 12 of 15

String Operations

String Functions

Perl has many built-in functions for manipulating strings.

Case Conversion

say uc("hello");    # HELLO
say lc("HELLO");    # hello

Substring

substr extracts part of a string:

my $str = "Hello, World!";
say substr($str, 0, 5);    # Hello
say substr($str, 7);       # World!

String Length

say length("Perl");   # 4

Finding Substrings

index returns the position of a substring (-1 if not found):

my $pos = index("Hello, World!", "World");
say $pos;   # 7

Sprintf

sprintf formats strings (like C's sprintf):

my $msg = sprintf("Name: %s, Age: %d", "Alice", 30);
say $msg;

Your Task

Write a subroutine shout that takes a string and returns it in uppercase. Print the result of calling shout with "hello".

JS Transpiler loading...
Loading...
Click "Run" to execute your code.