# String manipulation in Cocoa



## strikeman (Mar 28, 2002)

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?


----------



## Ghoser777 (Mar 28, 2002)

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...


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

So, the general form is:


```
#define functionName(parameter1,parameter2,...) annoyingCodeThatIsWayTooMuchToTypeOverAndOverAgain
```

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

HTH,
F-bacher


----------



## iconara (Apr 7, 2002)

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) containsNSString *) substring
- (int) indexOfNSString *) substring
- (int) indexOfNSString *) substring fromOffset: (int) offset
- (int) lastIndexOfNSString *) substring
- (int) lastIndexOfNSString *) substring fromOffsetint) 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


----------



## maccatalan (Jul 30, 2002)

> _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


----------



## maccatalan (Jul 30, 2002)

about NSRange ...

I found it!! 

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


----------



## maccatalan (Jul 30, 2002)

NS : this page about Range is from the Java API and not the Obj-C one.
Does somebody understand why ? ... that's so strange ...


----------



## maccatalan (Jul 30, 2002)

I got it!  

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


----------



## iconara (Jul 30, 2002)

exciting, four posts in 30 minutes... and you found the answer yourself. good work! =)


----------



## kainjow (Jul 31, 2002)

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:
	
	



```
left(string, 5)
```
So then in ObjC I was thinking you could do something like
	
	



```
[string left:5];
```
Simple? I haven't started on this, but maybe later...


----------



## maccatalan (Jul 31, 2002)

> _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.  
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  )


----------



## maccatalan (Jul 31, 2002)

heuu ... I just remembered that some functions allowing that already exist in the NSString class :

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

... ooops!  ... I'm really absent-minded  

have fun!
Pierre


----------

