📜  珀尔 |字符串函数(长度、lc、uc、index、rindex)

📅  最后修改于: 2022-05-13 01:55:44.817000             🧑  作者: Mango

珀尔 |字符串函数(长度、lc、uc、index、rindex)

Perl 中的字符串是用某种引号括起来的字符序列。 Perl字符串可以包含 UNICODE、ASCII 和转义序列字符。 Perl 提供了各种函数来像任何其他编程语言一样操作字符串。 Perl 的一些字符串函数如下:

  • 长度()
  • lc()
  • uc()
  • 指数()
  • 索引()

length():此函数用于查找字符串中的字符数。该函数返回字符串的长度。以下是说明此方法的程序。

  • 示例 1:
    # Perl program to demonstrate 
    # string length function
      
    # string
    my $s = "geeksforgeeks";
      
    # using length function &
    # displaying length
    print(length($s),"\n"); 
    

    输出:

    13
    
  • 示例 2:
    # Perl program to demonstrate 
    # string length function
      
    # string
    my $s = "#$%HeLLo CSHARP &+#*";
      
    # using length function &
    # displaying length
    print(length($s),"\n");  
    

    输出:

    19
    

lc():此函数返回字符串的小写版本。以下是说明此方法的程序。

  • 示例 1:
    # Perl program to demonstrate 
    # string lc function
      
    # string
    my $s = "GEEKSFORGEEKS\n";
      
    # using lc function &
    # displaying result
    print("To lower case: ");
    print(lc($s),"\n");
    

    输出:

    To lower case: geeksforgeeks
    
  • 示例 2:
    # Perl program to demonstrate 
    # string lc function
      
    # string
    my $s = "GEEKS\n";
      
    # using lc function &
    # displaying result
    print("To lower case: ");
    print(lc($s),"\n");
    

    输出:

    To lower case: geeks
    

uc():此函数返回字符串的大写版本。以下是说明此方法的程序。

  • 示例 1:
    # Perl program to demonstrate 
    # string uc function
      
    # string
    my $s = "geeksforgeeks";
      
    # using uc function &
    # displaying result
    print("To Upper Case: ");
    print(uc($s),"\n");
    

    输出:

    To Upper Case: GEEKSFORGEEKS
    
  • 示例 2:
    # Perl program to demonstrate 
    # string uc function
      
    # string
    my $s = "GeekS\n";
      
    # using uc function &
    # displaying result
    print("To Upper Case: ");
    print(uc($s),"\n");
    

    输出:

    To Upper Case: GEEKS
    

index():该方法将从字符串中的指定位置搜索子字符串,并返回该子字符串在字符串中第一次出现的位置。如果省略该位置,它将从字符串的开头开始搜索。此方法将采用两个参数,即原始字符串和必须搜索的子字符串。

例子 :

# Perl Program to illustrate 
# the index() function
  
# !/usr/bin/perl
use warnings;
use strict;
  
# string
my $st = "GeeksforGeeks\n";
  
# substring
my $subs = "for";
  
# using index function
my $r = index($st, $subs);
  
# displaying result
print(qq\The substring $subs found at position $r in string $st\);

输出:

The substring for found at position 5 in string GeeksforGeeks

rindex()此函数与 index() 相同,只是它返回字符串中最后一次出现的文本。此外,可以给出第三个参数,它返回该位置之前或该位置的位置。它从字符串的末尾而不是从开头搜索。以下是说明此方法的程序。

示例 1:

# Perl Program to illustrate 
# the rindex() function
  
# !/usr/bin/perl
use warnings;
use strict;
  
# string
my $st = "GeeksforGeeks\n";
  
# substring
my $subs = "for";
  
# using rindex function
my $r = rindex($st, $subs);
  
# displaying result
print(qq\The substring $subs found at position $r in string $st\);

输出:

The substring for found at position 5 in string GeeksforGeeks

示例 2:

# Perl Program to illustrate 
# the rindex() function with 
# three parameters
  
# !/usr/bin/perl
  
# using rindex() function 
$p = rindex("GeeksForGFGGeeksgeeksforGFG", "GFG");
  
print "Founded position of GFG $p\n";
  
# Use the first position found 
# as the offset to the next search.
# The length of the target string
# is subtracted from the offset 
# to save time.
$p = rindex("GeeksForGFGGeeksgeeksforGFG", "GFG", $p-7);
print "Founded position of GFG $p\n";

输出:

Founded position of GFG 24
Founded position of GFG 8