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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:21:22+00:00 2026-05-26T08:21:22+00:00

I want to manipulate NSString in obj-c here is what I want to do

  • 0

I want to manipulate NSString in obj-c here is what I want to do :

iterate a string though a for-each / for loop and shift left (<<) each character of NSString
but I don’t know how should I iterate through the NSString’s characters and how to use shift operator in obj-c.

I’m fairly new in objective-c .

regards

  • 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-26T08:21:22+00:00Added an answer on May 26, 2026 at 8:21 am

    NSStrings are immutable; mutableCopyWithZone: will get you an (implicitly retained) NSMutableString. However, NSMutableString doesn’t have a way of setting individual characters. It would be easier to get an array of characters using one of the many methods (e.g. getCharacters:range: for wide characters, or cStringUsingEncoding:, getCString:maxLength:encoding: or UTF8String for c-style strings), then operate on that (note some methods return const strings), then construct a new string using (e.g.) initWithCString:encoding:. Keep in mind that, depending on what you’re trying to accomplish, shifting bytes may not give you the result you expect, due to encoding issues and multibyte characters.

    You can get the length of a string using length, which is the number of characters in the string (also the size, in unichars, of a buffer to hold UTF-16 data, not including a null-terminator), or lengthOfBytesUsingEncoding:, which will tell you the size (number of bytes) needed for a buffer to hold the contents of the string (not including a null-terminator). maximumLengthOfBytesUsingEncoding: can also be used for a buffer size, though it may be larger than the actual necessary size. For variable-length encodings, the maximum size is the largest possible character size (e.g. 3 for UTF-8 encoded unichars) times the number of characters.

    Looping and shifting is otherwise the same as in C: initialize the index variable to the lower bound (0) and loop until the index variable exceeds the upper bound.

    NSUInteger i;
    NSString *result=nil;
    unichar *data;
    NSRange dataRange = {0,0};
    dataRange.length = [string length];
    if ((data = malloc(dataRange.length * sizeof(unichar)))) {
        [string getCharacters:data range:dataRange];
        for (i=0; i < dataRange.length; ++i) {
            // shiftAmount is declared elsewhere
            data[i] <<= shiftAmount;
        }
        result = [[NSString alloc] initWithCharacters:data length:dataRange.length];
    } else {
        // malloc() failed; handle error
        ...
    }
    

    If the data isn’t string data but bytes, NSData/NSMutableData would be more appropriate.

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

Sidebar

Related Questions

I want to manipulate views while in a for loop. I manipulate a view
I want to manipulate an array of integers in a function, but I am
I simply want to manipulate the elements within the #content div, but $('#content') doesn't
I want to manipulate xml doc having default namespace but no prefix. Is there
I want to manipulate HTML using a custom view engine like this: protected override
I want to manipulate .zip and .rar files from C++. What libraries should I
i want to manipulate xml data simple .. thanks
I want to manipulate a games roomlist, for filtering the maps. My idea was,
Say I want to manipulate some files on a floppy drive or a USB
I start with a basic class that I want to manipulate in a List

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.