• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Ruby / Using Ruby to Get the Length of a String

Using Ruby to Get the Length of a String

October 14, 2022 Leave a Comment

We can use Ruby to get the length of a String by using either the String length method or the size method. The length method will return the length of the string, and if the string is empty, it will return 0. Let’s take a look at the length method first.

string_length = "This is a String".length

The above code would return the number 16.

Some other examples of length method are below:

string_1 = "This-is-a-String".length;
string_2 = "ThisisaString".length;
string_3 = "".length;
string_4 = "This String has numbers 01239".length;

puts string_1
puts string_2
puts string_3
puts string_4

#Output
16
13
0
29

Let’s take a look at getting the String length in Ruby using the size method.

Using Ruby to Get String Length Using the Size Method

We can also use the size method in Ruby to get the length of a string, just as we used the length method above.

string_length = "This is a String".size

The above code would return the number 16.

Some other examples of size method are below:

string_1 = "This-is-a-String".size;
string_2 = "ThisisaString".size;
string_3 = "".size;
string_4 = "This String has numbers 01239".size;

puts string_1
puts string_2
puts string_3
puts string_4

#Output
16
13
0
29

Hopefully this article has been useful to help you understand how to use Ruby to get string length.

Other Articles You'll Also Like:

  • 1.  How to Print With Newline in Ruby
  • 2.  How to Square a Number in Ruby
  • 3.  Ruby unshift Method – Add Items to Start of Array
  • 4.  Using Ruby to Convert Array to String
  • 5.  Ruby has_key Method – Check if Hash Has Key
  • 6.  Using Ruby to Capitalize First Letter of String
  • 7.  Ruby Floor – Find Floor of Number with floor Method
  • 8.  Using Ruby to Get the Length of Array
  • 9.  Ruby has_value Method – Check if Hash Has Value
  • 10.  Get the Sum of an Array in Ruby

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy