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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:03:32+00:00 2026-05-16T17:03:32+00:00

Why is substringFromIndex not working for my NSMutableString ? Here code similar to what

  • 0

Why is substringFromIndex not working for my NSMutableString ?

Here code similar to what I have :

NSMutableString *myString = [[NSMutableString alloc] initWithString:@"This is my String"];

[myString substringFromIndex:5];

NSLog(@"myString = %@", myString); //will output This is my String

If I use substringFromIndex on NSString it will work, for example like so :

NSString *tempStr = [[NSString alloc] init];
tempStr = [myString substringFromIndex:5]; 
NSLog(@"tempStr = %@", tempStr); //will output is my String 

Why does it not work in the first example, and I have one more question, if I do it using the second method, and then I set:

[myString setString:tempStr];
[tempStr release];

This will result in a crash, I thought, since I used setString on NSMutableString, that I do not need the NSString and I release it, but apparently that is not the case, however if I use autorelease it will be OK

  • 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-16T17:03:33+00:00Added an answer on May 16, 2026 at 5:03 pm

    That method never alters the string you call it on. It returns a new string in both cases. So assign it to a new string variable and your good.


    It’s crashing because you over releasing one object and and leaking another. You alloc the first string, then make a new autoreleased string from substringFromIndex:, then release it. You dont need to try this hard.

    Simply assign the output of the substring method to a variable, and let it be autoreleased for you. No alloc, no release.


    A full proper example might look like this:

    NSMutableString *myString = [[NSMutableString alloc] initWithString:@"This is my String"];
    NSString *tmpString = [myString substringFromIndex:5];
    NSLog(@"tempStr = %@", tempString);
    [myString setString:tempStr];
    
    // later do [myString release]
    

    or even simpler:

    NSString *myString = @"This is my String";
    myString = [myString substringFromIndex:5];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

if i have an NSString eg-> string(102)?xml etc How to remove all chars upto
Okay maybe i just need another set of eyes on this, but I have
In the following code, I have determined that everything works, up until [tableView reloadData]
I'm puzzled... I have this function colorWithHexString... when I include it in the viewcontroller
Both work, but which one would you use and why? @implementation NSString (Extender) -(NSString
I get an ' EXC_BAD_ACCESS ' error when trying to access variable in a
I'm parsing some input which produces a tree structure containing NSDictionary instances on 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.