How to use the rstrip string method in Julia
Overview
The rstrip method in Julia removes the specified characters from the trailing end (right side) of a given string.
Syntax
rstrip(sourcestr::AbstractString, charsToBeRemoved)
Arguments
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.
Return value
This method returns a new string by removing the specified trailing characters from the given string.
Code example
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']))
Code explanation
Line 2: We use the rstrip method with the string, with:
winneras 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:
winneras 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:
winneras 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.