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 8464909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:53:30+00:00 2026-06-10T14:53:30+00:00

Let string_a = I’m a very long long long string Is there a method

  • 0

Let string_a = "I'm a very long long long string"

Is there a method in Ruby that truncates a string something like this?:

magic_method(string_a) # => "I'm a ver...ng string"

In Rails I can do: truncate(string_a) but only the first part is returned.

  • 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-06-10T14:53:31+00:00Added an answer on June 10, 2026 at 2:53 pm

    You can try the method String.scan with a regular expression to break the string where you wish. Try somenthing like:

    def magic_method(string_x)
        splits = string_x.scan(/(.{0,9})(.*?)(.{0,9}$)/)[0]
        splits[1] = "..." if splits[1].length > 3
        splits.join
    end
    

    Explanation:
    Supose string_a = "I'm a very long long long string"; string_b = "A small test string"; and string_c = "tiny". First split the string within 3 groups.

    • The first group (.{0,9}) try to catch the first 9 or less characters. Ex. "I'm a ver" for string_a; "A small t" for string_b and "tiny" for string_c.
    • The last group (.{0,9}$) try to catch the last 9 or less characters and the end of string ($). Ex."ng string" for string_a; "st string" for string_b and "" for string_c;
    • The middle group (.*?) try to catch what is left over. This only works because of the ? that makes this regular expression not greedy (otherwise it would get the rest of the string, lefting nothing to the last group. Ex: "y long long lo" for string_a, "e" for string_b and "" for string_c

    Than we check if the middle group is greater than 3 characters, if so, we replace with "...". This will only happen on string_a. Here we wouldn’t like to make string_b longer, replacing "e" with "..." resulting in "A simple t...st string".

    Finally join the groups (array elements) into a single string with Array.join

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let string_a = I'm a string but this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa is long. How is it
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do
Let's say I have a string like this: String test = hfikoebndolahsdHEL123LOkjahhsdqhuihs; And then
Let's say that I have a string 5a . This is the hex representation
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Let's suppose there is a piece of code like this: my $str = 'some
Let's say I've a string 12345 I should obtain all subsequence combinations of this
Let's say I have a string This $0 is $2 $1. Still, \$$3 is
Let's say I have this code in Java: HashSet<String> wordSet = new HashSet<String>(); String
I have let's say this string: A = Beta * C / Delta +

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.