fig09_04.pl

来自「PERL语言资料 可以用于PERL程序设计」· PL 代码 · 共 68 行

PL
68
字号
#!/usr/bin/perl
# Fig. 9.4: fig09_04.pl
# Demonstrating basic string functions.

use warnings;
use strict;

my $string = "hello there\n";
print "The original string: ", 'hello there\n', "\n\n";

# Using substr
print "Using substr with the string and the offset (2): ";
print substr( $string, 2 );
print "Using substr with the string, offset (2) and length (3): ";
print substr( $string, 2, 3 ), "\n";
print "Using substr with offset (-6), and length (2): ";
print substr( $string, -6, 2 ), "\n";
print "Using substr with offset (-6) and length (-2): ";
print substr( $string, -6, -2 ), "\n\n";

# replace first 5 characters of $string with "bye"
# assign substring that was replced to $substring
my $substring = substr( $string, 0, 5, "Bye" );

print "The string after the replacement: $string";
print "The substring that was replaced: $substring\n\n";

# convert all letters of $string1 to uppercase
$string = uc( $string );
print "Uppercase: $string";

# convert all letters of $string to lowercase
$string = lc( $string );
print "Lowercase: $string \n";

# only change first letter to lowercase
$string = lcfirst( $string );
print "First letter changed to lowercase: $string";

# only change first letter to uppercase
$string = ucfirst( $string );
print "First letter changed to uppercase: $string \n";

# calculating the length of $string
my $length = length( 'Bye there\n' );
print "The length of \$string without whitespace: $length \n";

$length = length( "Bye there\n" );
print "The length of \$string with whitespace: $length \n";

$length = length( $string );
print "The length of \$string (default) is: $length \n";

###########################################################################
#  (C) Copyright 2001 by Deitel & Associates, Inc. and Prentice Hall.     #
#  All Rights Reserved.                                                   #
#                                                                         #
#  DISCLAIMER: The authors and publisher of this book have used their     #
#  best efforts in preparing the book. These efforts include the          #
#  development, research, and testing of the theories and programs        #
#  to determine their effectiveness. The authors and publisher make       #
#  no warranty of any kind, expressed or implied, with regard to these    #
#  programs or to the documentation contained in these books. The authors #
#  and publisher shall not be liable in any event for incidental or       #
#  consequential damages in connection with, or arising out of, the       #
#  furnishing, performance, or use of these programs.                     #
###########################################################################

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?