Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ruby
delete
communitycreator

What is the string.delete method in Ruby?

okwudili onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The string.delete method is used to delete characters in a string. This method deletes the specified character or characters in the string that call it.

Syntax

str.delete other_string

Parameters

str: This is the string that we want to delete some characters from.

other_string: This string contains the specified characters that we want to delete.

Return value

The value returned is a new string that does not contain the deleted characters.

Code example

# create a string
str1 = "Edpresso"
str2 = "is"
str3 = "very"
str4 = "awesome"
# call the delete function
a = str1.delete "so" # delete characters "s" and "o"
b = str2.delete "i" # delete character "i"
c = str3.delete "ve" # delete characters "v" and "e"
d = str4.delete "wae" # delete characters "w", "a" and "e"
# print returned values
puts a
puts b
puts c
puts d
Delete some characters from a string using the "string.delete" function in Ruby

Code explanation

In the code given above, we create some strings and delete some of their characters, using the string.delete method.

RELATED TAGS

ruby
delete
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring