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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:47:30+00:00 2026-05-25T18:47:30+00:00

i would like to remove the last line from my string.. the nsstring object

  • 0

i would like to remove the last line from my string.. the nsstring object here is _text. My thoughts are to scan characters from the end and if i found a carriage return symbol i substring to that index, and finnish. But i don’t want to remove every carriage return, just the last one.

So i would like to do something like this:

for (int i = [_text length] ; i>0; i--) {
    char character = [_text characterAtIndex:i];
    if (character == @"\n") {
       _text = [_text substringToIndex:i];
     return;
    }
}

Any help would be very appreciated!
Thanks.

  • 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-25T18:47:31+00:00Added an answer on May 25, 2026 at 6:47 pm

    You should handle the last char being whitespace or CR.
    You also had a bug where you needed length – 1 in the for loop.

    Here’s some working code:

        NSString *_text = [NSString stringWithString:@"line number 1\nline number 2\nlinenumber 3\n  "];
    
        // make sure you handle ending whitespace and ending carriage return
        _text = [_text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
        NSUInteger i;
        unichar cr = '\n';
        for (i = [_text length] - 1; i>0; i--) 
        {
            if ([_text characterAtIndex:i] == cr) 
            {
                break;
            }
        }
    
        if (index > 0)
        {
            _text = [_text substringToIndex:i];
        }
    
        NSLog(@"%@", _text);
    

    This outputs:
    2011-09-22 08:00:10.473 Craplet[667:707] line number 1
    line number 2

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

Sidebar

Related Questions

I have a MS SQL table McTable with column BigMacs nvarchar(255). I would like
I've been advised to remove the return command from my bashrc file in order
Is there a Python script or tool available which can remove comments and docstrings
I'm currently writing a tool that parses the FxCop logfile on a large codebase.
I'm cleaning an incoming text in my Java code. The text includes a lot
I needed a utililty function earlier today to strip some data out of a
I'm working on Comet support for CppCMS framework via long XMLHttpRequest polls. In many
What I am doing is creating a simple html page in which there is
I am working on a uni project and our goal is to make a
I'm using Session variables inside a web service to maintain state between successive method

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.