Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 5, 2005, 02:58 PM   #1
grapes911
Demi-God (Moderator emeritus)
 
grapes911's Avatar
 
Join Date: Jul 2003
Location: Citizens Bank Park
String to Char to String

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
grapes911 is offline   0 Reply With Quote
Old Apr 5, 2005, 03:18 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
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).
robbieduncan is offline   0 Reply With Quote
Old Apr 5, 2005, 03:24 PM   #3
grapes911
Thread Starter
Demi-God (Moderator emeritus)
 
grapes911's Avatar
 
Join Date: Jul 2003
Location: Citizens Bank Park
Thanks. I'm going to try it later tonight.

PS. Why did I know you were going to be the one to respond?
grapes911 is offline   0 Reply With Quote
Old Apr 5, 2005, 03:27 PM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by grapes911
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.
robbieduncan is offline   0 Reply With Quote
Old Apr 6, 2005, 02:34 PM   #5
grapes911
Thread Starter
Demi-God (Moderator emeritus)
 
grapes911's Avatar
 
Join Date: Jul 2003
Location: Citizens Bank Park
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]) {
...
}
grapes911 is offline   0 Reply With Quote
Old Apr 6, 2005, 04:40 PM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by grapes911
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.
robbieduncan is offline   0 Reply With Quote
Old Apr 6, 2005, 04:55 PM   #7
Mr. Durden
macrumors 6502a
 
Join Date: Jan 2005
Location: Colorado
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.
Mr. Durden is offline   0 Reply With Quote
Old Apr 7, 2005, 08:52 AM   #8
grapes911
Thread Starter
Demi-God (Moderator emeritus)
 
grapes911's Avatar
 
Join Date: Jul 2003
Location: Citizens Bank Park
Quote:
Originally Posted by robbieduncan
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.
grapes911 is offline   0 Reply With Quote
Old Apr 7, 2005, 09:39 AM   #9
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by grapes911
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.
robbieduncan is offline   0 Reply With Quote
Old Apr 7, 2005, 11:07 AM   #10
grapes911
Thread Starter
Demi-God (Moderator emeritus)
 
grapes911's Avatar
 
Join Date: Jul 2003
Location: Citizens Bank Park
Quote:
Originally Posted by robbieduncan
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.
grapes911 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
changing NSTextView string value abcdefg12345 Mac Programming 4 Feb 5, 2014 06:50 AM
Format string is not a string literal. larswik Mac Programming 5 Jan 17, 2014 10:45 PM
Search a string for a String of a specific format. ncaissie iPhone/iPad Programming 19 Nov 15, 2013 10:42 AM
string shortcuts duden32 OS X 10.8 Mountain Lion 3 May 16, 2013 02:34 AM
String Encoding manga iPhone 1 Jan 8, 2013 12:04 AM

Forum Jump

All times are GMT -5. The time now is 11:00 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC