Ruby program to pad a string with 0's on left side

Ruby Example: Write a program to pad a string with 0's on left side.
Submitted by Nidhi, on January 02, 2022

Problem Solution:

In this program, we will create a string and pad the string with 0's on the left side using the rjust() method.

Program/Source Code:

The source code to pad a string with 0's on the left side is given below. The given program is compiled and executed successfully.

# Ruby program to pad a string with 
# 0's on the left side

str = "12345";

rPadStr = str.rjust(10,"0");

print "Right justified string padded with 0's in left side: ",rPadStr;

Output:

Right justified string padded with 0's on left side: 0000012345

Explanation:

In the above program, we created a string str initialized with "12345". Then we used the rjust() method to pad the string from the left side with a specified character and return the updated string. After that, we printed the updated string.

Ruby Strings Programs »


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.