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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:24:26+00:00 2026-06-09T23:24:26+00:00

I have a huge string being prepared by using << operator in a loop.

  • 0

I have a huge string being prepared by using << operator in a loop. At the end I want to delete the last 2 chars.

some_loop
  str << something
end
str = str[0..-3]

I think the last operation above would consume memory and time as well, but I’m not sure. I just wanted to see if there is an operation with the opposite effect of << so I can delete those 2 last chars from the same string.

  • 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-09T23:24:28+00:00Added an answer on June 9, 2026 at 11:24 pm

    In fact, string slicing is already a fast and memory efficient operation as the string content isn’t copied until it’s really necessary.

    See the detailed explanation at “Seeing double: how Ruby shares string values“.

    Note that this is a somewhat classical optimization for string operations; You have it in java too and we often used similar tricks in C.

    So, don’t hesitate to do:

    str = str[0..-3]
    

    That’s the correct, recommended and efficient way, provided you really have to remove those chars, see Sergio’s answer.

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

Sidebar

Related Questions

I have a huge string (22000+ characters) of encoded text. The code is consisted
I have a huge string that looks like this: Text Text Text Text Text
I have the following problem, from c++ I send huge string[] to java. huge
string str1 = 12345ABC...\\...ABC100000; // Hypothetically huge string of 100000 + Unicode Chars str1
some thing like the above image. I have a huge list of data being
I have a huge pile of php scripts with lots of constants being used
I have a huge string. I need to extract a substring from that that
I have a map defined like this std::map<some_key_type, std::string::iterator> mIteratorMap; And a huge string
At the moment I have code that is looking through a huge string. It
I have a huge vector of boost dynamic_bitset . I want to write the

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.