String manipulation in Cocoa

strikeman

Registered
Just started learning Cocoa after a 10 year break from Mac programming, and something was readily apparent:

Cocoa sucks at string manipulation

When I say this, I don't mean that it isn't powerful, but rather that it is really inconvenient. I have spent a few months with PHP, and it is unbelievably easy to build up strings:

$s = "static text $variables inserted or appended ".$another_var;

With Cocoa you have to futz around with stringByAppendingString, formats, etc.

Have I just been spoiled by PHP? Or am I missing some basic capabilities of Cocoa string manipulation?
 
You've just been spoiled. The way perl (heck even java!) deals with Strings is a lot easier. But not all is lost! You can do simple define statements to make your life a lot easier. I'm not great at this, so let's see if I remember...

Code:
#define concat(x,y) [x stringByAppendingString:y]

So, the general form is:

Code:
#define functionName(parameter1,parameter2,...) annoyingCodeThatIsWayTooMuchToTypeOverAndOverAgain

Define as many convenience methods as you like, and your life will be a lot easier.

HTH,
F-bacher
 
at first I too thought that the cocoa-libraries did not have any useful string manipulating functions, but I changed my mind. they are not like java (which has good string manipulating functions in my opinion), to get over this I wrote a category (extension) to the NSString class to include the methods:

- (BOOL) contains:(NSString *) substring
- (int) indexOf:(NSString *) substring
- (int) indexOf:(NSString *) substring fromOffset: (int) offset
- (int) lastIndexOf:(NSString *) substring
- (int) lastIndexOf:(NSString *) substring fromOffset:(int) offset

which are the functions I use most frequently. the NSString-methods available are rangeOfString: and the struct NSRange, which in my opinion is a cumbersome way of doing small things.
I would like regexps as well, but I think someone else should do that... omnigroup has actually done it in one of their frameworks, but I never got that to work...

I have included the ILStringSeach.m-file, but you'll have to create the header yourself (just remove the method bodies and change some other things.

I hope it still works, there are two autorelease messages I'm not so sure about, but that's about all. the code is quite simple.

[<b>note:</b> apparently omniweb4.1b2 can't upload files... bummer. if you want the category, I can email you the source-files.]

theo
 
Originally posted by iconara
if you want the category, I can email you the source-files.

I'm trying to deal with strings ... the problem is that I don't understand what NSRange is ... there is no documentation to describe the NSRange class in Obj-C. :(

I'm really interested by your category. If you could post it there or send it by email (maccatalan@mac.com) ...

thank you,
Pierre
 
about NSRange ...

I found it!! :D

It is not listed in the Foundation classes nor into the classes index of a project, but you can find it into the Apple Cocoa Documentation : /Cocoa Developper Documentation/Data Management/Geometry and Range utilities/NSRange

;)

But I'm still interested by the sources of your mutated NSString ;)
 
NS : this page about Range is from the Java API and not the Obj-C one.
Does somebody understand why ? ... that's so strange ...
 
I got it! :D

The NSRange is not a Obj-C class but a simple C struc like this :

typedef struct _NSRange {
unsigned int location;
unsigned int length;
} NSRange;

That's why there's no class definition ... since there's no class. :)

sorry for all those messages. :)

... but I'm still interested by your NSString derivated class ;)
 
I was thinking about writing a category thing that would let you use functions in ObjC that would be similar to REALbasic (for REALbasic to ObjC programmers). For example, in REALbasic, to get the first 5 characters of a string, you simply go:
Code:
left(string, 5)
So then in ObjC I was thinking you could do something like
Code:
[string left:5];
Simple? I haven't started on this, but maybe later...
 
Originally posted by kainjow
Simple? I haven't started on this, but maybe later...

yes ;)

It should be something like this :

- (NSString *) left: (int) a {
return [self substringWithRange:NSMakeRange(0,a)];
}

- (NSString *) right: (int) a {
return [self substringWithRange:NSMakeRange([self length]-a,a)];
}

... the harder is to understand what NSRange is : a simple INxIN number where the first digit is the start index of your range, and the second one its length. :D
It's not a NSObject subclass just a classical C struct with two int fields, location and length. :)

have fun!
(I'm so happy to have understood this about NSRange :cool: )
 
heuu ... I just remembered that some functions allowing that already exist in the NSString class :

[myString substringFromIndex:anInteger];
[myString substringToIndex:anInteger];

... ooops! :p ... I'm really absent-minded :D

have fun!
Pierre
 
Back
Top