The rstrip
method in Julia removes the specified characters from the trailing end (right side) of a given string.
rstrip(sourcestr::AbstractString, charsToBeRemoved)
This method takes two arguments:
sourcestr
: This is the string from which the trailing characters are to be removed.chars
: These are the characters that are to be removed.This method returns a new string by removing the specified trailing characters from the given string.
The code below demonstrates how we can use the rstrip
method:
# Removing the character 'r' at the endprintln(rstrip("winner", ['r']))# Removing the characters 'r', 'n', 'e', at the endprintln(rstrip("winner", ['r', 'n', 'e']))# Removing the character 'g' at the endprintln(rstrip("winner", ['g']))
Line 2: We use the rstrip
method with the string, with:
winner
as the source string['r']
as the character to be removed from the end of the string.The rstrip
method loops the source string from the end and removes the character that matches 'r'
. The loop is stopped when a character of the source string is not present in the removable characters. In our case, we get winnne
as a result.
Line 4: We use the rstrip
method with the string, with:
winner
as the source string['r', 'n', 'e']
as the characters to be removed from the end of the string.The rstrip
returns wi
as a result.
Line 6: We use the rstrip
method with the string, with:
winner
as the source string['g']
as the character to be removed from the end of the string.The rstrip
returns winner
as a result. The character 'g'
is not present in the source string.