Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7278055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:49:23+00:00 2026-05-28T22:49:23+00:00

I want my string in groups of 5 characters, .e.g. thisisastring => [thisi, satri,

  • 0

I want my string in groups of 5 characters, .e.g.

thisisastring => ["thisi", "satri", "ng"]  

but I also want the last group to be padded with __’s, e.g.

thisisastring => ["thisi", "satri", "ng___"]  

I have got as far as the string splitting:

"thisisastring".scan /.{5}/)

["thisi", "satri", "ng"]

but not too sure how to do the padding for that last group to make it “ng___”

although starting to think that combinations of dividend (div()), modulus (%) and .ljust might do it.

Maybe number of padding characters would be: (length % 5) * "_" (if you can multiply that) ?

Perhaps something that uses:

ruby-1.9.2-p290 :023 > (len % 5).to_i.times { print '_' }
___ => 3
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T22:49:24+00:00Added an answer on May 28, 2026 at 10:49 pm

    Since the adjustment is only required on the last element, it is more effective to do the adjustment before splitting rather than itterating over the elements to do adjustment.

    ("thisisastring"+"_"*("thisisastring".length % 5)).scan(/.{5}/)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have strings in the form [abc].[some other string].[can.also.contain.periods].[our match] i now want to
I have a string from which i want to extract 3 groups: '19 janvier
I have a string 2045111780&&-3&5&-7 I want a regex to give me groups as:
I want to pass string as a parameter in with my jni. but when
Question: I want to write a custom aggregate function that concatenates string on group
i have a simple query in Linq which gets the characters group by word
Given a string containing some number of square brackets and other characters, I want
I want to replace all non-alpha characters in a string with a plus '+'
I want to remove all - and / characters in a date string. Can
I have a long string. string s1 = '99024','99050','99070','99143','99173','99191','99201','99202','99203','99204','99211','99212','99213','99214','99215','99217','99218','99219','99221','99222','99231','99232','99238','99239','99356','99357','99371','99374','99381','99382','99383','99384','99385','99386','99391','99392'; I want string s2 =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.