Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lstrip
string
julia
communitycreator

How to use the lstrip string method in Julia

Programming Bytes

Overview

The lstrip method in Julia removes the specified characters from the start to the end of a given string.

Syntax

lstrip(sourcestr::AbstractString, charsToBeRemoved)

Arguments

This method takes two arguments.

  1. sourcestr: This is the string from which the leading characters are to be removed.
  2. chars: These are the characters that are to be removed.

Return value

This method returns a new string by removing the specified characters from the given string.

Code example

The code below demonstrates how we can use the lstrip method:

# Getting a stripped part of the given string
println(lstrip("winner", ['w']))

# Removing the characters 'w', 'n', 'i' from start of the string
println(lstrip("winner", ['w', 'n', 'i']))

# Removing the character 'g' from the start of the string
println(lstrip("winner", ['g']))
Using the lstrip method

Code explanation

Line 2: We use the lstrip method with the string, with:

  • winner as the source string
  • ['w'] as the character to be removed from the start of the string.

The lstrip method loops the source string from the start, and removes the character that matches ‘w’. The loop is stopped when a character of the source string is not present in the string. In our case, we get innner as the result.

Line 4: We used the lstrip method with the string, with:

  • winner as the source string
  • [‘w’, ‘n’, ‘i’] as the characters to be removed from the end of the string.

The lstrip returns re as the result.

Line 6: We use the lstrip method with the string, with:

  • winner as the source string
  • ['g'] as the character to be removed from the end of the string.

The lstrip returns winner as the result. The character g is not present in the source string.

RELATED TAGS

lstrip
string
julia
communitycreator
RELATED COURSES

View all Courses

Keep Exploring