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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:51:22+00:00 2026-05-16T15:51:22+00:00

I want to replace multiple elements in my string in Objective-C. In PHP you

  • 0

I want to replace multiple elements in my string in Objective-C.

In PHP you can do this:

str_replace(array("itemtoreplace", "anotheritemtoreplace", "yetanotheritemtoreplace"), "replacedValue", $string);

However in objective-c the only method I know is NSString replaceOccurancesOfString. Is there any efficient way to replace multiple strings?

This is my current solution (very inefficient and.. well… long)

NSString *newTitle = [[[itemTitleField.text stringByReplacingOccurrencesOfString:@"'" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@"'"] stringByReplacingOccurrencesOfString:@"^" withString:@""];

See what I mean?

Thanks,
Christian Stewart

  • 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-16T15:51:23+00:00Added an answer on May 16, 2026 at 3:51 pm

    If this is something you’re regularly going to do in this program or another program, maybe make a method or conditional loop to pass the original string, and multi-dimensional array to hold the strings to find / replace. Probably not the most efficient, but something like this:

    // Original String
    NSString *originalString = @"My^ mother^ told me not to go' outside' to' play today. Why did I not listen to her?";
    
    // Method Start
    // MutableArray of String-pairs Arrays
    NSMutableArray *arrayOfStringsToReplace = [NSMutableArray arrayWithObjects:
                                               [NSArray arrayWithObjects:@"'",@"",nil], 
                                               [NSArray arrayWithObjects:@" ",@"'",nil], 
                                               [NSArray arrayWithObjects:@"^",@"",nil], 
                                               nil];
    
    // For or while loop to Find and Replace strings
    while ([arrayOfStringsToReplace count] >= 1) {
        originalString = [originalString stringByReplacingOccurrencesOfString:[[arrayOfStringsToReplace objectAtIndex:0] objectAtIndex:0]
                                                  withString:[[arrayOfStringsToReplace objectAtIndex:0] objectAtIndex:1]];
        [arrayOfStringsToReplace removeObjectAtIndex:0];
    }
    
    // Method End
    

    Output:

    2010-08-29 19:03:15.127 StackOverflow[1214:a0f] My'mother'told'me'not'to'go'outside'to'play'today.'Why'did'I'not'listen'to'her?
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a string like this: abc%\%%%% I want to replace multiple %%%
Suppose I have this: My---sun--is------very-big---. I want to replace all multiple hyphens with just
I have a string that contains multiple spaces. I want to replace these with
I want to replace all "%" with "%%" in my string. However, my string
I am trying to replace multiple values in a string with JS replace(). The
I want to replace multiple newline characters with one newline character, and multiple spaces
I want to replace all multiple slashes in URL, apart from those in protocol
I'm trying to match replace a multiple lines block with sed and I can't
I want to replace the value in an attribute in a XML. This attribute
I want replace a(or each) element(number) instead another element(number) in a class while running

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.