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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:24:17+00:00 2026-06-08T22:24:17+00:00

I have a feeling this will be too wordy, but I’ll do my best.

  • 0

I have a feeling this will be too wordy, but I’ll do my best. My app has quotes built-in that change each day. Some of them are over 140 characters, but I would like to be able to share via Twitter, so I need a way to get the count, and if over 140, edit it. What I have so far is:

int maxChars = 140;
        int charsLeft = maxChars - [label1.text length];

        NSString *removed = [label1.text substringToIndex:[label1.text length] -  charsLeft];


        TWTweetComposeViewController* twc = [[TWTweetComposeViewController alloc] init];
        [twc setInitialText:removed];
        [self presentModalViewController:twc animated:YES];

Where label1 is the UILabel that shows the quote. This is throwing an error on quotes over 140 characters

[__NSCFString substringToIndex:]: Range or index out of bounds'

Any thoughts? One other thing I was thinking. Each quote ends with

" - Person who said it

I was thinking I could get the character count, remove the excess characters + 3 and insert an … before the -. How could I go about doing this, or at least fix my existing code?

  • 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-08T22:24:18+00:00Added an answer on June 8, 2026 at 10:24 pm

    Well, just imagine your code with a label that has 10 characters in it.

     int maxChars = 140;
    
     int charsLeft = maxChars - [label1.text length];
    
     NSString *removed = [label1.text substringToIndex:[label1.text length] -  charsLeft];
    

    becomes

     int charsLeft = 140 - 10; //charsLeft = 130
    
     NSString *removed = [label1.text substringToIndex:10 -  130];
    

    This would tell SubstringToIndex to get the characters from 0 to -120, which doesn’t make very much sense.

    It might be tricky to split your string to isolate the author of the quote because both quotes and author names might include hyphens. If you want to try it anyway, you could do something like one of the answers to this spring-parsing question suggest (http://stackoverflow.com/questions/2166809/number-of-occurrences-of-a-substring-in-an-nsstring) for the string @” – “, and try and isolate the last instance.

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

Sidebar

Related Questions

I have a feeling this will be a quick fix, given that I started
I have a really strong feeling that this question will either be closed or
I have a feeling this is a stupid question but I can't find the
I have a feeling that this query is pretty easy to construct, I just
I have a feeling I'm going about this all wrong. But anyway. I have
I have the feeling the answer to this question is no but I figured
I have a feeling I am completely borking this MySQL query but I'll ask
I somehow have the feeling that modern systems, including runtime libraries, this exception handler
This doesn't make sense to me, but I have a feeling I saw a
Keep going around circles, but I am still unclear about this. Have a feeling

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.