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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:12:11+00:00 2026-05-16T05:12:11+00:00

Taking substrings of a string is a very common string manipulation operation, but I

  • 0

Taking substrings of a string is a very common string manipulation operation, but I heard that there might be considerable differences in performance/implementation between the Java and .NET platform. Specifically I heard that in Java, java.lang.String offers constant time operation for substring, but in .NET, System.String offers linear performance Substring.

Are these really the case? Can this be confirmed in the documentation/source code, etc? Is this implementation specific, or specified by the language and/or platform? What are the pros and cons of each approach? What should a person migrating from one platform to another look for to avoid falling into any performance pitfalls?

  • 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-16T05:12:11+00:00Added an answer on May 16, 2026 at 5:12 am

    In .NET, Substring is O(n) rather than the O(1) of Java. This is because in .NET, the String object contains all the actual character data itself1 – so taking a substring involves copying all the data within the new substring. In Java, substring can just create a new object referring to the original char array, with a different starting index and length.

    There are pros and cons of each approach:

    • .NET’s approach has better cache coherency, creates fewer objects2, and avoids the situation where one small substring prevents a very large char[] being garbage collected. I believe in some cases it can make interop very easy too, internally.
    • Java’s approach makes taking a substring very efficient, and probably some other operations too

    There’s a little more detail in my strings article.

    As for the general question of avoiding performance pitfalls, I think I should have a canned answer ready to cut and paste: make sure your architecture is efficient, and implement it in the most readable way you can. Measure the performance, and optimise where you find bottlenecks.


    1 Incidentally, this makes string very special – it’s the only non-array type whose memory footprint varies by instance within the same CLR.

    2 For small strings, this is a big win. It’s bad enough that there’s all the overhead of one object, but when there’s an extra array involved as well, a single-character string could take around 36 bytes in Java. (That’s a “finger-in-the-air” number – I can’t remember the exact object overheads. It will also depend on the VM you’re using.)

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

Sidebar

Related Questions

I have a string that might be between 1 and 8 characters long. I
Taking a rails tutorial, and I've run into the following problem that I'm having
To explain what I'm doing, I'm basically taking a string of X's and O's
I am taking a line from a file only if that file doen't have
I need to replace a dynamic substring withing a larger string, but only once
Taking the following string example, what is the pattern I should use to extract
Is there a method that tests if 2 URLs are equal, ie point to
I'm trying to implement something that finds the common suffix between a number of
I am now taking parts of string like this: something.Substring(0, something.Length >= 8 ?
Taking this file as an example, I'm trying to read the data in a

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.