PDA

View Full Version : String to Char to String




grapes911
Apr 5, 2005, 02:58 PM
I'm forced to do a project in Objective-C (I usually use C/C++ or Java), so this may seem like a dumb or basic question.

I have two NSStrings and a char:

// This is the string containing the file's text
// path was defined earlier and this seems to work
myString = [[NSString alloc] initWithContentofFile:Path];

// This is the string I want to create
NSString *tempString = [[NSString alloc] initWithString:@""];

// temp char to put 1st char of myString into
char *tempChar;

I want to take the first character of myString and put it into tempChar. I'm going to test tempChar and then either discard it or put it to the end of tempString.

So to make it easy, can someone give me some lines of code that:
1. Set tempChar to the first char of myString
2. Remove the first letter of myString but keep the rest of the string
3. Add the char in tempChar to the end of tempString

Thanks for any help



robbieduncan
Apr 5, 2005, 03:18 PM
Problem 1: NSStrings are immutable (i.e. cannot be changed). Use NSMutableString for the string you are going to add to.

Problem 2: Cocoa is Unicode based (this is a good thing). Do not use char, use unichar (this allows for 2-byte characters).

You can get a character (unicode) with [myString characterAtIndex:index]; where index is an int, zero based.

You can append to the mutable string with [myMutableString appendFormat:@"%C",char]; where char is a unichar.

If you only want to use 8-bit characters and normal C you can convert to and from standard C strings (i.e \0 terminated char[]) using the methods in NSString.

So to answer your questions:
NSString *myString = [[NSString alloc] initWithContentsOfFile:Path];
NSMutableString *tempString = [[NSMutableString alloc] initWithCapacity:[myString length]];
int i;
unichar c;
for (i=0;i<[myString length];i++)
{
c = [myString characterAtIndex:i];
if (<TEST c here...>)
{
[tempString appendFormat:@"%C",c];
}
}

Seems to compile for me (with Path replaced with @"" and the fake test replaced with 1).

Edit to add: note that myString is not altered. We do not remove anything from it (as it's immutable, the only way to remove the first character would be to create a new object which can be costly).

grapes911
Apr 5, 2005, 03:24 PM
Thanks. I'm going to try it later tonight.

PS. Why did I know you were going to be the one to respond? ;)

robbieduncan
Apr 5, 2005, 03:27 PM
Thanks. I'm going to try it later tonight.

PS. Why did I know you were going to be the one to respond? ;)

No problem. I'm actually glad to see people trying to get to grips with Cocoa. It's not that different in many ways to Java (i.e. strings are immutable in Java too, it just hides it better). I'm normally a Java developer too (well, Java/Perl/Shell/Sybase/...) but I honestly think Cocoa and Objective-C are better for GUI desktop apps.

grapes911
Apr 6, 2005, 02:34 PM
Another Question.
My comparisons aren't working. I need to compare the unichar with a NSString. Is this possible? Or should I do this another way.

c is a unichar
s is a nsstring

if ([c isEqualTo:s]) {
...
}

robbieduncan
Apr 6, 2005, 04:40 PM
Another Question.
My comparisons aren't working. I need to compare the unichar with a NSString. Is this possible? Or should I do this another way.

c is a unichar
s is a nsstring

if ([c isEqualTo:s]) {
...
}

A character cannot equal a string! Is your NSString a single character? If so use the method above to remove the first (and only) character from the string and compare them that way. Otherwise tell me what you want to do (for example are you trying to see if the character is in the string, in a range of characters, for example a white-space character or what).

Edit to add: you have made a kind of basic error here. c is not an object. It's a basic type (like char, int and so on). So you can't pass it messages like a subclass of NSObject.

Mr. Durden
Apr 6, 2005, 04:55 PM
Well, I've never made of chair out of string, but I think if you wrapped the string tight enough and with good knots... Hey, wait a second. You spelled chair wrong... Oh. Oh, I see. Carry on then.

grapes911
Apr 7, 2005, 08:52 AM
A character cannot equal a string! Is your NSString a single character? If so use the method above to remove the first (and only) character from the string and compare them that way. Otherwise tell me what you want to do (for example are you trying to see if the character is in the string, in a range of characters, for example a white-space character or what).

Edit to add: you have made a kind of basic error here. c is not an object. It's a basic type (like char, int and so on). So you can't pass it messages like a subclass of NSObject.


I've got it. I've found out about scanner. It made things much easier. Next time I have a problem, maybe I should just say what I need to do and someone will point me to the right methods. Thanks for the help.

robbieduncan
Apr 7, 2005, 09:39 AM
I've got it. I've found out about scanner. It made things much easier. Next time I have a problem, maybe I should just say what I need to do and someone will point me to the right methods. Thanks for the help.

Perhaps. I personally think that by playing around with the string methods you might have learnt a few things that will be usefull going forward.

grapes911
Apr 7, 2005, 11:07 AM
Perhaps. I personally think that by playing around with the string methods you might have learnt a few things that will be usefull going forward.

Thats true. I did learn some things. I really like learning new languages, but for some reason I still perfer to code in C++. I hoping to get more into cocoa developement though.