Remove WhiteSpaces from String.

Discussion in 'iOS Programming' started by TalShani, Jul 19, 2010.

  1. macrumors member

    Joined:
    Jun 29, 2010
    #1
    Hello guys,
    I have a TextField that collects String,
    Like if I write at the TextField "Hello" and press the button,
    It will be like wordOne = TextField.text
    I need to check if the string ends with WhiteSpace,
    If yes, remove it and present to the user the wordOne without the space, at the TextField,
    And if not, to show the user the wordOne without any change, at the TextField.
    Example: if I have string "hey. ", I want to show to the user "hey."
    Someone can tell me how can I do it?

    One more question - How do I capitalize the first word at the sentence? (With code)
    Example: "hello" = "Hello".

    Thanks,
    Tal Shani
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A306 Safari/6531.22.7)

    No instance method of NSString looks like it might do what you want?
     
  3. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #3
    No, All the method I tried crushed the app, do you have any idea of how I can do it?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Post what you have tried and the crash details.
     
  5. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #5
    Code:
    NSString *newString = [oldString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
    
    This one, its didnt crush the app, when Im trying the do
    TextField.text=(@"@%",newString)
    the TextField delete itself (not showing any text)
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    is not even close to making sense. You cannot create a NSString with a format by just using some brackets. And even if you could your format string is wrong.
    And even if it wasn't that doesn't really make any sense anyway. The only time you'd normally do something like this is with NSLog.
     
  7. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #7
    OK, so how can I do it?
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    I'm not about to write your code for you. The text property of TextField (Objective-C convention is to start variables with a lower case BTW so normally this would be textField) expects a NSString. newString is a NSString. I can see no reason for trying to create another string. The one you have is just fine. If you really want to create another string using a format call the appropriate class method on NSString.

    Given that you have tried this at all I suggest you need to brush on on your C (so you actually know what the statement you wrote does) and then Objective-C. If I were you I'd do that before writing any more code as this is a fundamental lack of understanding of the very basics of the language.
     
  9. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #9
    I only said I don't now how to remove the last WhiteSpace at the end of a string,
    I didn't say I need help learning the language, I have a full app ready to be submit into the app store and all I go is this annoying bug.
    I tried to read something that could help me on the documentation but I can't find something like that.
    I know the language and I read full of articles about removing the WhiteSpaces,
    But somehow the methods they suggest don't really works for me,
    so I came here to ask about method I can use,
    but all you do is giving me the same things I already told you I got,
    I don't need to create new string,
    my whole app based on one string that passes some cases and if's and then present itself to the user again.
    All I'm asking is some method you recommend (as a developer) to use for solving my kind of problem,
    I'm not asking for you to write my code, just give me one method that I can use properly.
    I'm hope you will answer me soon,
    Tal Shani.
     
  10. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    If you read my first paragraph I did answer your question.

    My suggestion to learn the language better was based on the code you posted. It demonstrates a fundamental lack of understanding of basic C, let alone Objective-C.
     
  11. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #11
    I didn't write the code I published earlier, someone published it in other forum,
    For the same problem I have, so I just tried it, it didn't work and I gave you it as example to something i saw and tried, I know Objective-C well and I already wrote a code to remove all other characters that's not letters (A-Z) or numbers (1-9), so !,@@%^ and all of this characters, I succeeded to delete them, but the WhiteSpace don't remove itself because somehow it says its okay.
    So can you give me a method to do what I need?
     
  12. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #12
    I assume you mean that it does not remove whitespace characters from the middle of the string? This is to be expected. The documentation (which I expect you to have read before posting here) says:

    "Returns a new string made by removing from both ends of the receiver characters contained in a given character set."

    Given that you want to remove all whitespace characters I would suggest a combination of componentsSeparatedByCharactersInSet:, stringByTrimmingCharactersInSet: and componentsJoinedByString:.

    I would expect anyone who claims to "know Objective-C well" would be able to find this in the documentation themselves and construct the very basic algorithm to do what they want. Programming is about identifying the problem, breaking it down into sub-problems and solving them. It's not about using Google to find code from people who can actually solve problems to copy and paste.
     
  13. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #13
    I'm already using "componentsSeparatedByCharactersInSet:",
    The string I want to remove the WhiteSpace from is already a WORD (and a WORD only! - "Hello. " or something like that).
    I tried to do:
    Code:
    wordFinal = [wordFinal stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    and then present it:
    Code:
    takeText.text = (@"@%",wordFinal);
    And all it does is the same thing, make the text field equals to @"".
    Do I have any mistake on writing the code?
    Its like the same code as before, since I cant find other way to do it |:
    Tal.
     
  14. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #14
    Code:
    takeText.text = (@"@%",wordFinal);
    As I already said this makes no sense at all. What do you think (as you "know Objective-C well" this should not be hard) this is doing? Explain this line of code in simple, clear English.
     
  15. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #15
    takeText.text = My text field.
    @% = Keeper for the variable wordFinal.
    So actually it says to the text field to save place,
    and then give the place the variable wordFinal.
    NOTE: This is equal to takeText.text = wordFinal (same result).
     
  16. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #16
    1) @% is not what you think it is. %@ is the format placeholder for an Objective-C object

    2) What do you think the brackets are doing? They certainly don't create an object, allow you to put a string into a format or anything similar.
     
  17. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #17
    I fixed it, it still don't working, the takeText.text says "".
     
  18. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #18
    Then use the debugger to step through and find the error.
     
  19. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #19
    There's the problem, there is NO error, the app works find, just the text field don't showing anything.. can you help me build a command that should remove the space (if there is one) with no problem?
    Thanks, Tal.
     
  20. macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #20
    Well holy crap, I didn't expect such a thing to compile, yet alone do anything sane. But I did a simple test...

    Code:
    NSString *test1 = @"Test String";
    NSString *test2 = (@"%@",test1);
    NSString *test3 = test1;
    
    Which spit out
    Code:
    2010-07-20 15:38:06.137 otest[3459:903] test1: Test String, test2: Test String, test3: Test String
    It seems a little pointless but it does seem to work. So either something funky is going on in the memory that makes my test work by sheer luck, or there is a different problem. My first guess is that maybe your takeText object is nil, so setting the text on it does nothing?

    On a side note, while this
    Code:
    NSString *string = (@"%@',newstring)
    does seem to work, I think it is in very bad form. It is unnecessary and only confuses the code. When I look at a string assignment I would expect it to be trivial. This method is not trivial, first I had to do a double take when I saw the line just to understand what it was attempting to do. Second, I didn't believe that it would do what I expected it to do without writing some test code of my own. Hopefully you can see why
    Code:
    NSString *string = newstring;
    is better. Just because of this line this thread has gotten pretty long without addressing the problem. I second the debugger suggestion, I imagine there is something going wrong elsewhere in you code.
     
  21. macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #21
    Also, just because there are no compile time errors, doesn't mean there are no errors. Undefined behavior is a giant error.
     
  22. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #22
    there is all work just fine, the takeText is working well at analyzing the text and removing other letters, like !@$^&*(), the biggest problem is when a user type-in "Hello. ", its make the app crush.
    Do you have e-mail or MSN or Skype that I can explain you the problem with screen sharing or something?
     
  23. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #23
    Do you even understand the difference between @% and %@ ?
     
  24. thread starter macrumors member

    Joined:
    Jun 29, 2010
    #24
    sure.. -.-
     
  25. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #25
    Alright. What is it then?
     

Share This Page