Working with strings in Obj-C and Cocoa

iconara

Registered
Coming from Java, Obj-C/Cocoa seems to lack good support for working with strings, is this true or am I missing something?

What I want to do is assemble this input:

<blockquote>
<tt>
2620<br>
0K .......... .......... ..<br>
........ .......... ...<br>
....... 77%<br>
@ 6.9<br>
8 MB/s<br>
</tt>
</blockquote>

(I have to check for a newline before and after, since there are more lines like this one... and yes you are right, it's output from wget)

into this string:

<blockquote>
<tt>26200K .......... .......... .......... .......... .......... 77% @ 6.98 MB/s</tt>

</blockquote>

and then take out these values (as NSNumbers):

<blockquote>
26200
77
6.98
</blockquote>

Doing this in Java is easy using indexOf() and substring(), but NSString does not have these methods...



regards
Theo
 
A total lack of proper String functions seems to be a classic virtue of C. Java is incredibly well equipped to handle sttrings. Although NSString does some stuff, I found that I needed about half as much code to do my string manipulation in Java.

Like you, I feel as though I must be missing something, but I've felt that way about C ever since I had to stop programming in Pascal.
 
Omnigroup's frameworks include a regular expression engine. I've never tried it, though. If it works, I'd love to hear how well it works, and how easy to use it is. I tried another regular expression framework for Cocoa and it came with no documentation, so I gave up and wrote a perl program -- calling it from within my program, then parsing the cleaner output.
 
rangeOfCharacterFromSet: is what you want for indexOf(). substringWithRange: is what you want for substring(). They do the equivalent, although the name is some what misleading.

HTH,
F-bacher
 
tie>>

when one resorts to solutions like that, I think one should doubt that one is doing is the right thing. calling a command-line process just to handle strings is not very compatible nor is it fast. it's just about the slowest thing there is. objective-c's string support isn't that bad...

I think I should look in to OmniFoundation, it's a shame that it is not thoroughly documented.


theo
 
Originally posted by iconara
tie>>

when one resorts to solutions like that, I think one should doubt that one is doing is the right thing. calling a command-line process just to handle strings is not very compatible nor is it fast. it's just about the slowest thing there is. objective-c's string support isn't that bad...

Well, I was actually doing more complicated things than just indexOf or substring. And my programming time was the priority, not the running time. But I agree, it was an ugly hack.
 
as any old unix codger would tell you, the only ugly hack is a hack that doesn't work. If it got the job done, and allowed the programmer to get on to other things, then the system and the programmer worked together to get a solution with minimal work.

Machines are fast, they're supposed to do grossly inefficient things at 7 times the speed of light so the programmer doesn't have to think as hard. If optimal code was a priority, we'd have more assembly programmers than java programmers. I'm happy the system offered a quick and familiar way for you to get your job done.

All the same, thanks for the tips on NSString manipulation. :)
 
another thing about NSString-ops: if one sends the message rangeOfString:mad:"a string" to a string, and that string does not contain "a string", what does the message return? I don't get consistent values... I have gotten 2675009 (just making it up, I think it's the max value of an integer) and (-2781761, still making up, think it's the min value of an integer), but I am not sure what this means and how to check if the string did not contain the string I searched for.


theo
 
Maybe you can check to see if the value is out of the range of your NSSTring object? I bet it always returns something out of range when it can't find an appropriate index.

HTH,
F-bacher
 
Hey all. I'm just programming my first Cocoa project now, in Objective-C. I am 15, BTW. But anyway...

To see if the string passed to rangeOfString: doesn't exist, just have a look at its return value; it returns an NSRange, which is a structure containing "location" and "length". Just check to see if "Length" is zero, and then you're happy. (Well, I am. :) )

if ([someString rangeOfString:anotherString].length == 0)
// 'someString' doesn't contain 'anotherString'
 
well hidden in the documentation one can find that the NSRange returned from -rangeOfString: will have NSNotFound as as the value of the location variable, meaning:

if( [aString rangeOfString:mad:"anotherstring"].location != NSNotFound ) {
NSLog( @"astring contained anotherstring" );
}

yeah! apple documentation sucks.
 
Back
Top