Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

chomp
string
perl
communitycreator

What does the chomp() method do to Strings in Perl?

okwudili onyejiaku

Overview

The chomp() method, when called on a string, removes the last trailing newline from the string.

Syntax

chomp(str)

Parameters

str: The string we want to remove from any last trailing new line.

Return value

This method returns the total number of trailing newlines removed.

Example

# create some strings
$greetings = "Hi! Welcome to Ruby!\n";
$name = "Ruby Programming Language\n";
# use the chomp method
$result1 = chomp($greetings);
$result2 = chomp($name);
$result3 = chomp($greetings, $name);
# print results
print "$result1 \n";
print "$result2 \n";
print "$result3";
Using the chomp() method on Strings in Perl

Explanation

In the above code snippet:

  • Lines 2 and 3: We create two strings.
  • Lines 5 to 7: We use the chomp method and remove trailing newline characters in the strings we created.
  • Lines 9 to 11: We print the results to the console.

RELATED TAGS

chomp
string
perl
communitycreator
RELATED COURSES

View all Courses

Keep Exploring