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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:43:34+00:00 2026-05-20T04:43:34+00:00

I want to split a NSString into NSStrings that have no more than 75

  • 0

I want to split a NSString into NSStrings that have no more than 75 octets (in UTF8 representation) each.

If my NSString would be completely ascii it would be a no-brainer.

But since the resulting string could have any length between 18 and 75 characters I have no real idea how to do it.

what’s the way to do this?

Convert the string into octets, take the first 75, convert it back to NSString and hope that NSString tells me that I’ve ripped an utf8-character into two parts?

  • 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-20T04:43:35+00:00Added an answer on May 20, 2026 at 4:43 am

    the answer of warren showed me that I was on the right track. This is what I came up with:

    - (NSString *)foldString:(NSString *)string withOctetCount:(NSInteger)octetCount {
        NSMutableString *output = [NSMutableString string];
        NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
        NSInteger convertedBytes = 0;
        while (convertedBytes < [stringData length]) {
            NSString *subString = nil;
            NSInteger usedLength = octetCount;
            if (convertedBytes > 0) {
                // all lines after the first one get a space as prefix. so use one octet less in those lines
                usedLength--;
            }
            if (convertedBytes + usedLength > [stringData length]) {
                usedLength = [stringData length] - convertedBytes;
            }
            while (!subString) {
                NSData *data = [stringData subdataWithRange:NSMakeRange(convertedBytes, usedLength)];
                subString = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
                if (!subString) {
                    usedLength--;
                    if (usedLength == 0) {
                        // TODO: remove abort
                        abort();
                        return nil;
                    }
                }
            }
            //      NSLog(@"Used %d octets", usedLength);
            if (convertedBytes == 0) {
                // Dont prefix with space on first line
                [output appendString:subString];
            }
            else {
                [output appendFormat:@"\r\n %@", subString];
            }
            convertedBytes += usedLength;
        }
        return output;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to split my list into rows that have all the same number
I want to split up the jQuery .js file into two, but I have
I have a menu of product brands that I want to split over 4
I received an NSString from the server. Now I want to split it into
I have a string that represents a path to a directory. I want split
I have a report in Access that we want split and exported to .rtf
I have a NxN matrix which I want to split into non-overlap KxK block.
I want to split an arithmetic expression into tokens, to convert it into RPN.
Let's say I have a split view, and I want to fill half of
I want split integers into their factors. For example, if the total number of

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.