← Back to Perl Course | Chapter 3: Strings | Lesson 5 of 7

Case, Trimming और Characters

Small built-ins case बदलते हैं, line endings trim करते हैं, और characters swap करते हैं।

Case, Trimming, और Characters

uc, lc, ucfirst, और lcfirst एक string का case बदलते हैं, और scalar context में reverse इसके characters reverse करता है। chomp एक trailing newline remove करता है और chop last character remove करता है। tr/// operator characters को एक-एक करके translate करता है और, scalar context में, कितने characters match हुए यह return करता है।

Note: chomp सिर्फ newline remove करता है ($/ की value), इसलिए इसे call करना safe है भले ही वहाँ कोई newline न हो।

उदाहरण: Case, trimming and characters

perl
use strict;
use warnings;

my $line = "  perl programming  \n";
chomp $line;
print "chomped: [$line]\n";
(my $trimmed = $line) =~ s/^\s+|\s+$//g;
print "trimmed: [$trimmed]\n";
print "uc: ", uc($trimmed), "\n";
print "ucfirst: ", ucfirst($trimmed), "\n";
print "reversed: ", scalar reverse($trimmed), "\n";
(my $dna = "ATGC") =~ tr/ATGC/TACG/;
print "complement: $dna\n";
my $vowels = ($trimmed =~ tr/aeiou//);
print "vowels: $vowels\n";
my $word = "chopper";
chop $word;
print "chopped: $word\n";

# Output:
# chomped: [  perl programming  ]
# trimmed: [perl programming]
# uc: PERL PROGRAMMING
# ucfirst: Perl programming
# reversed: gnimmargorp lrep
# complement: TACG
# vowels: 4
# chopped: choppe
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. chop इस्तेमाल करना जब आप सिर्फ एक newline remove करना चाहते थे
  2. यह उम्मीद करना कि list context में reverse "abc" letters reverse करेगा
  3. chomp से spaces trim करने की कोशिश करना
चैप्टर सारांश
  • uc, lc, ucfirst, और lcfirst case बदलते हैं
  • chomp एक trailing newline remove करता है
  • scalar reverse एक string reverse करता है
  • tr/// characters translate या count करता है
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.