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

  • Home
  • SEARCH
  • 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 9031675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T07:47:27+00:00 2026-06-16T07:47:27+00:00

I need to shorten a string in a loop before passing it again and

  • 0

I need to “shorten” a string in a loop before passing it again and again to be matched by a java.regex.Pattern. Probably a trivial situation for some deeply involved in parsing and text processing.

I am faced with the situation that I have to use:

string=string.substring(shortenHowMuch); //Need to shorten it from the beginning

…which low level developers very well know, that is copying the entire string into another address of the memory. Even with HotSpot’s optimisations in place (which I am aware of), I still need to make sure the code runs in maximum possible performance variant on all possible Java VMs.

Edit:
Later I found out, that my statement above, that it is copying the string, is wrong. So my very question should go “to hell” 🙂 Anyway, read if you like 🙂 .substring does not copy, but instead it does share a reference to a char[], very interesting 🙂

  • 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-16T07:47:29+00:00Added an answer on June 16, 2026 at 7:47 am

    You did not explain what exactly happened when you used StringBuilder so I cannot answer why. But actually you don’t need StringBuilder because String.substring is optimized and it does not copy the internal char array. This is what happens internally

    public String substring(int beginIndex, int endIndex) {
            ..... 
            return ((beginIndex == 0) && (endIndex == count)) ? this :
                new String(offset + beginIndex, endIndex - beginIndex, value);
     }
    
    // Package private constructor which shares value array for speed.
    String(int offset, int count, char value[]) {
        this.value = value;
        this.offset = offset;
        this.count = count;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some help. I am trying to shorten a full product name from
Say we have a UTF-8 string $s and we need to shorten it so
I need to take a string, and shorten it to 140 characters. Currently I
For directory-naming algorithm, need to strip vowels (and white space) from string to shorten
I need to shorten a string down.. Lets say we have a string with
I have a blog I wrote in PHP, and need to shorten the latest
I need to have a navigation on which some links (those who are shorter
How can i shorter below codes than before? i need short and simple method:
Need some regular expressions help. So far I have my code working to allow
Need some help... I have jasperserver 4.1 installed on my ubuntu. It runs via

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.