String to Char to String

Discussion in 'Mac Programming' started by grapes911, Apr 5, 2005.

  1. Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #1
    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:mad:""];

    // 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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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:mad:"%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:mad:"%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).
     
  3. thread starter Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #3
    Thanks. I'm going to try it later tonight.

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

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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.
     
  5. thread starter Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #5
    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]) {
    ...
    }
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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.
     
  7. macrumors 6502a

    Joined:
    Jan 13, 2005
    Location:
    Colorado
    #7
    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.
     
  8. thread starter Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #8

    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.
     
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    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.
     
  10. thread starter Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #10
    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.
     

Share This Page