Search code examples
iosobjective-cuitextfieldwords

How do I replace words in a text field to change into other words in another text field?


This is for Xcode 5 and for iOS apps; I'm working with Objective-C code.

Okay so for example, if I put into the first text field "I was born in 1995." and when I click a convert button, I want it to say "I was conceived circa 1995." How can I go about doing that? The problem I'm having is being able to replace a word with another word.

Like, how do I return whats in the text field to be replace the possible words into whatever the person types in? Sort of like a translator app, the way it replaced words.

My question concerns that if the user were to type anything into the text field, then it would rephrase it for him with words that have synonyms to be other words.

Yes, it is about replacing substrings, but it will replace whatever the user types into it.

I understand that stringByReplacingOccurrencesOfString:withString: makes sense, but what would go after that to apply to whatever the user typed in?

Basically, let's say a translator app. If I type in: "I am very smart." it would rephrase to: "I am very intellectual." It has to deal with whatever the user types in.


Solution

  • If the string is in NSString, you can use the stringByReplacingOccurrencesOfString:withString: method.

    NSString *str = @"I was born in 1995.";
    
    str = [str stringByReplacingOccurrencesOfString:@"born in" withString:@"conceived circa"];
    

    Not sure though if this is what you mean.