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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:56:24+00:00 2026-06-06T17:56:24+00:00

I have a string that looks like string url = "www.example.com/aaa/bbb.jpg"; "www.example.com/" is 18

  • 0

I have a string that looks like

string url = "www.example.com/aaa/bbb.jpg";

"www.example.com/" is 18 fixed in length. I want to get the "aaa/bbb" part from this string (The actual url is not example nor aaa/bbb though, the length may vary)

so here’s what I did:

string newString = url.Substring(18, url.Length - 4);

Then I got the exception: index and length must refer to a location within the string. What’s wrong with my code and how to fix it?

  • 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-06T17:56:26+00:00Added an answer on June 6, 2026 at 5:56 pm

    The second parameter in Substring is the length of the substring, not the end index (in other words, it’s not the length of the full string).

    You should probably include handling to check that it does indeed start with what you expect, end with what you expect, and is at least as long as you expect. And then if it doesn’t match, you can either do something else or throw a meaningful error.

    Here’s some example code that validates that url contains your strings, that also is refactored a bit to make it easier to change the prefix/suffix to strip:

    var prefix = "www.example.com/";
    var suffix = ".jpg";
    string url = "www.example.com/aaa/bbb.jpg";
    
    if (url.StartsWith(prefix) && url.EndsWith(suffix) && url.Length >= (prefix.Length + suffix.Length))
    {
        string newString = url.Substring(prefix.Length, url.Length - prefix.Length - suffix.Length);
        Console.WriteLine(newString);
    }
    else
        //handle invalid state
    
    • 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 contains a url which looks like this: http://www.test.com/images/tony 's
i have an array that looks like: array(5) { [title]=> string(22) http://example.com/288 [title2]=> string(22)
I have a string that looks like this: <name>-<gender>-<age>.jpg I want to be very
I have a column with a url sting that looks like this http://www.somedomain.edu/rootsite1/something/something/ or
I have a String that looks like this String = Förpackning Flaska (375 ml)
I have a string that looks like this: 9/1/2009. I want to convert it
i have a string that looks like this: sodjfoisdfsdf sdofij sodiosifosf fsdi a123 sdfoi
Problem: I have a string that looks like this: [1=>2,3,4][5=>6,7,8][9=>10,11,12][13=>14,15][16=>17,18] Question: How can you
If I have a string that looks like either ./A/B/c.d OR .\A\B\c.d How do
I have a huge string that looks like this: Text Text Text Text Text

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.