Let’s know about String and Array Methods in Ruby to Clean and Format Your Data.

Let’s know about String and Array Methods in Ruby to Clean and Format Your Data.

22 December 2021

Ruby:It is a pure object-oriented language, a general-purpose and interpreted programming language. Ruby is an open-source language.

In this blog you will learn different types of string and array methods.

Let’s know the String methods in ruby.

Let’s-know-about-String-and-Array-Methods-in-Ruby-to-Clean-and-Format-Your-Data-image3-1024x575

String: The term “string” refers to a grouping of one or more characters. It could be made up of numbers, letters, or symbols.

Empty Method

Probably this method is used when we check empty strings for data validation. The ‘.empty’ method checks whether the string is empty or not.

# Example
string = “string method”
str = “”
1. string.empty?

=> false

2. str.empty?

=> true

 

Length Method

The ‘.length’ method is used to calculate the length of the string.

string = “string method”
string.length?
=>  13

Count Method

The ‘.count’ method counts the number of times a certain character appears in a string.

# Calculate the number of ‘l’ alphabet in the  given string
“Hello world”.count(‘l’)
=> 3

Upcase Method

The ‘.upcase’ method is used to convert the given string into the upper case.

string = “string method”
string.upcase
=> STRING METHOD

Downcase Method

The ‘.downcase’ method lowercases all of the letters in a string.

# Convert string into downcase
string = “String Method”
string.downcase
=>  string method

Swapcase Method

The ‘.swapcase’ method converts uppercase characters to lowercase and lowercase letters to uppercase in a string.

# Example 
string = “String Method”
string.swapcase
=> sTRING mETHOD

Capitalize Method

The ‘.capitalize’ method converts a string’s first letter to uppercase and the rest to lowercase.

# Example
String = ‘hello world ‘

String.capitalize

=> Hello World

Split Method

The ‘.split’ method is used to split a string into an array and then it returns the array.

# Example
“Hi, How are you?”.spilt
=> [“Hi”, “How”, “are”, “you?”]

Chop Method

The ‘.chop’ method is used to remove the last letter from the string.

# Example
“String”.chop
=> String

Chop! Method

The ‘.chop!’  method is used when we don’t want to remove the last letter from the string and want to keep it as it is.

# Example
name = “Chiku”
name.chop!
name == “Chiku”
=> true

Gsub Method

The ‘.gsub’ method is used to replace some of the previous text of the string with the new one.

# Example
“Hi, How are you?”.gsub(“Hi”, “Hello”) 
=> Hello, How are you?

Concatenation of the String

In ruby, to concatenate the string we used the ‘.concat’ method, “+” method, and “<<”  method.

# Example
1. “20” + “20” => 2020

2. “11” << ”11” => 1111

3. “11”.concat ”11” => 1111

 Insert a String

The ‘.insert’ method inserts a string before a certain index into another string.

# Example
 “Hi?”.insert(2, “ii”)
=> Hiii

Clear the String

To clear the string content, we used the ‘.clear’ method.

# Example
string = “Hi”
string.clear
=> “”

Reverse String

The ‘.reverse’ method reverses the sequence of the character of the string.

# Example
name = “chiku”
name.reverse
=> ukihc

Find string with given suffix and prefix

To check the string, start with a specific character.

# Example
string = “Hello, how are you?”

# Find string given suffix

string.start_with(“Hello”)

=> true

# Find string given prefix

sting.end_with(“you?”)

=> true

 Add a string before another string

We used the ‘.prepend’ method to add a string before another string.

# Example

String = World”

string.prepend(“Hello”)

=> Hello World

Let’s know the Array methods 

Let’s-know-about-String-and-Array-Methods-in-Ruby-to-Clean-and-Format-Your-Data-image2

Length method

It calculates the length of the array and returns the count of elements present in the array.

# Example

Array = [1, 2, 3, 4, 5]

Array.length

=> 5

First Method

To list the first element of the array. We used this method.

# Example

array = [1, 2, 3, 4, 5]

array.first

=> 1

Last Method

To list the last element of the array. We used this method.

# Example

array = [1, 2, 3, 4, 5]

array.last

=> 5

Take Method

This method returns the first n elements of the array.

# Example

array = [1, 2, 3, 4, 5]

array.take(3)

=> [1, 2, 3]

Drop Method

This method returns the elements after an n element of the array.

# Example

array = [1, 2, 3, 4, 5]

array.drop(3)

=> [4, 5]

Pop Method

The pop method removes the array’s last element.

# Example

array = [1, 2, 3, 4, 5]

array.pop

=> [1, 2, 3, 4]

Push Method

This method allows you to add elements at the end of an array.

# Example

array = [1, 2, 3, 4, 5]

array.push(6)

=> [1, 2, 3, 4,5, 6]

Shift Method

It removes the first element of the array.

# Example

array = [1, 2, 3, 4, 5]

array.shift

=> [2, 3, 4, 5]

Unshift Method

This method allows you to add an element at the beginning of the array.

# Example

array = [1, 2, 3, 4, 5]

array.pop

=> [1, 2, 3, 4]

Delete Method

This method allows you to delete specific elements from the array, which you want to delete from the array, you need to pass the index of the array element which you want to delete from the array.

# Example

array = [1, 2, 3, 4, 5]

array.delete(2)

=> [1, 2, 4, 5]

Reverse Method

This method allows you to reverse your array element. But this method reverses the array element temporarily.

# Example

array = [1, 2, 3, 4, 5]

array.reverse

=> [5, 4, 3, 2, 1]

Include Method

This method allows you to check if an array consists of a given element or not.

# Example

array = [1, 2, 3, 4, 5]

array.include?(6)

=> false

Flatten Method

The flatten method can be used to produce a one-dimensional array from an array that contains nested arrays.

# Example

array = [[11, 22], [ 33, 44], [55, 66]]

array.flatten

=> [11, 22, 33, 44, 55, 66]

Each Method

This method allows you to iterate over each element of the array.

# Example

array = [1, 2, 3, 4, 5]

array.each do |elements|

 puts elements

end

=>

1

2

3

4

5

Concat Method

This method allows you to concat two or more arrays.

# Example

array = [1, 2, 3, 4, 5]

array.concat[[44, 55], [11, 22]]

=> [1, 2, 3, 4, 5, 44, 55, 11, 22]

Uniq Method

This method removes the duplicate element from the array and returns a unique array element.

# Example

array = [1, 2, 3, 4, 5]

array.include?(6)

=> false

Join Method

The.join method returns a string containing all of the array’s members separated by the separator parameter.

# Example

array = [1, 2, 3, 4, 5]

array.join

=> 12345

search
Request a quote