简体   繁体   中英

Objective-C string manipulation

Currently I am working on a piece of software, which is currently written 100% in Objective-C using the iPhone 3.0 SDK.

I have come to a cross roads where I need to do quite a bit or string concatenation, more specifically, NSString concatenation and so far I have been doing it like this:

Objective-C string concatenation:

NSString *resultantString = (NSMutableString*)[@"Hello " stringByAppendingString: @"World"];

Now as you can imagine this gets quite difficult to read when I try to concatenate 6 NSStrings together.

At first I contemplated mixing in an Objective-C++ class to do my string concatenation and hand it back to my Objective-C class as then I could use C++'s easy string concatenation like:

C++ string concatenation:

string cppString = "Hello" + "World" + "see" + "easy!";

I could use C char arrays but that would be a little more difficult to read.

It then struck me that I could use a Python or Ruby bridge in Cocoa (which provide the added bonus of Regular expressions and superior string handling than the C based languages do).

This made sense to me even though I have coded only small amounts of Ruby and Python, because they are billed as string manipulation languages. Perl I decided to skip because it isn't directly supported in Xcode.

I am also interested in performance, so I am looking for the fastest way to do my string concatenation.

So what should I do? Is there some deviously easy way I am missing in Objective-C to concatenate many strings at once say 10 strings? or is my idea to use Python or Ruby class methods that return a concatenated or regex modified strings not as incredibly insane as it sounds? Perhaps I even missed some another way to do this?

Update: Yes. It seems I was rather crazy for thinking of pulling in another language runtime to do string manipulation, especially since I noted that I was concerned with speed. Adding a bridge would probably be much slower than simply using NSString/NSMutableString.

For fixed size concatenation, you can use [NSString stringWithFormat:] like:

NSString *str = [NSString stringWithFormat:@"%@ %@ %@",
                                  @"Hello", @"World", @"Yay!"];

you can use join operation.

NSArray *chunks  = ... get an array, say by splitting it;
string = [chunks componentsJoinedByString: @" :-) "];

would produce something like oop :-) ack :-) bork :-) greeble :-) ponies

Have you seen the appendString method from the NSMutableString class?

appendFormat from the same class will let you do many concatenations with one statement if that is what you're really interested in.

I would avoid mixing languages, particularly if you don't know Python or Ruby well. What you gain in readable code in your Objective-C you will loose have having to read multiple languages to understand your own code base. Seems like a maintainability nightmare to me.

I'd strongly suggest taking one of the suggestions of how to do this in Objective-C directly.

Dragging in C++ just for this seems quite heavy-handed. Using stringWithFormat: on NSString or appendFormat: with an NSMutableString, as others have suggested, is much more natural and not particularly hard to read. Also, in order to use the strings with Cocoa, you'll have to add extra code to convert back and forth from C++ strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM