Perl | substr() function Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report substr() in Perl returns a substring out of the string passed to the function starting from a given index up to the length specified. This function by default returns the remaining part of the string starting from the given index if the length is not specified. A replacement string can also be passed to the substr() function if you want to replace that part of the string with some other substring. This index and length value might be negative as well which changes the direction of index count in the string. For example, if a negative index is passed then substring will be returned from the right end of the string and if we pass the negative length then the function will leave that much of the characters from the rear end of the string. Syntax: substr(string, index, length, replacement) Parameters: string: string from which substring is to be extracted index: starting index of the substring length: length of the substring replacement: replacement substring(if any) Returns: the substring of the required length Note: The parameters 'length' and 'replacement' can be omitted. Example 1 Perl #!/usr/bin/perl # String to be passed $string = "GeeksForGeeks"; # Calling substr() to find string # without passing length $sub_string1 = substr($string, 4); # Printing the substring print "Substring 1 : $sub_string1\n"; # Calling substr() to find the # substring of a fixed length $sub_string2 = substr($string, 4, 5); # Printing the substring print "Substring 2 : $sub_string2 "; Output : Substring 1 : sForGeeks Substring 2 : sForG Example 2 Perl #!/usr/bin/perl # String to be passed $string = "GeeksForGeeks"; # Calling substr() to find string # by passing negative index $sub_string1 = substr($string, -4); # Printing the substring print "Substring 1 : $sub_string1\n"; # Calling substr() to find the # substring by passing negative length $sub_string2 = substr($string, 4, -2); # Printing the substring print "Substring 2 : $sub_string2 "; Output : Substring 1 : eeks Substring 2 : sForGee Comment More infoAdvertise with us Next Article Perl | Useful String functions A A_K_Mishra Follow Improve Article Tags : Perl Similar Reads Perl | split() Function split() is a string function in Perl which is used to split or you can say to cut a string into smaller sections or pieces. There are different criteria to split a string, like on a single character, a regular expression(pattern), a group of characters or on undefined value etc.. The best thing abou 8 min read Perl | sprintf() Function sprintf() function in Perl uses Format provided by the user to return the formatted string with the use of the values in the list. This function is identical to printf but it returns the formatted string instead of printing it. Syntax: sprintf Format, List Returns: a formatted scalar string Example 1 min read Perl | rindex() Function rindex() function in Perl operates similar to index() function, except it returns the position of the last occurrence of the substring (or pattern) in the string (or text). If the position is specified, returns the last occurrence at or before that position. Syntax: # Searches pat in text from given 2 min read JSTL fn:substring() Function In JSTL, the fn:substring() function is used to extract or retrieve the part of a given input or specified string, by starting from the start index to the end index [optional]. This function is mainly used for string manipulation tasks by many developers. In this article we will explore the Syntax a 2 min read Perl | Useful String functions A string in Perl is a scalar variable and start with a ($) sign and it can contain alphabets, numbers, special characters. The string can consist of a single word, a group of words or a multi-line paragraph. The String is defined by the user within a single quote (â) or double quote (â). Perl provid 3 min read Perl | ord() Function The ord() function is an inbuilt function in Perl that returns the ASCII value of the first character of a string. This function takes a character string as a parameter and returns the ASCII value of the first character of this string. Syntax: ord string Parameter: This function accepts a single par 1 min read Like