PDA

View Full Version : Remove WhiteSpaces from String.




TalShani
Jul 19, 2010, 05:22 PM
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



dejo
Jul 19, 2010, 06:18 PM
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?

TalShani
Jul 20, 2010, 03:21 AM
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?

No, All the method I tried crushed the app, do you have any idea of how I can do it?

robbieduncan
Jul 20, 2010, 03:55 AM
Post what you have tried and the crash details.

TalShani
Jul 20, 2010, 03:59 AM
Post what you have tried and the crash details.

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)

robbieduncan
Jul 20, 2010, 04:02 AM
This
TextField.text=(@"@%",newString)
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.

TalShani
Jul 20, 2010, 04:04 AM
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.

OK, so how can I do it?

robbieduncan
Jul 20, 2010, 04:08 AM
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 (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:) 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 (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:). 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.

TalShani
Jul 20, 2010, 04:23 AM
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 (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:) 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 (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithFormat:). 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.

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.

robbieduncan
Jul 20, 2010, 04:27 AM
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.

TalShani
Jul 20, 2010, 04:35 AM
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.

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?

robbieduncan
Jul 20, 2010, 04:44 AM
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: (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByCharactersInSet:), stringByTrimmingCharactersInSet: and componentsJoinedByString: (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/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.

TalShani
Jul 20, 2010, 05:06 AM
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: (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByCharactersInSet:), stringByTrimmingCharactersInSet: and componentsJoinedByString: (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/instm/NSArray/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.

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:
wordFinal = [wordFinal stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
and then present it:
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.

robbieduncan
Jul 20, 2010, 05:24 AM
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.

TalShani
Jul 20, 2010, 05:30 AM
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.

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).

robbieduncan
Jul 20, 2010, 05:47 AM
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).

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.

TalShani
Jul 20, 2010, 06:05 AM
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.

I fixed it, it still don't working, the takeText.text says "".

robbieduncan
Jul 20, 2010, 06:44 AM
I fixed it, it still don't working, the takeText.text says "".

Then use the debugger to step through and find the error.

TalShani
Jul 20, 2010, 07:01 AM
Then use the debugger to step through and find the error.

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.

seepel
Jul 20, 2010, 08:50 AM
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).

Well holy crap, I didn't expect such a thing to compile, yet alone do anything sane. But I did a simple test...


NSString *test1 = @"Test String";
NSString *test2 = (@"%@",test1);
NSString *test3 = test1;


Which spit out
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
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
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.

seepel
Jul 20, 2010, 08:52 AM
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.

Also, just because there are no compile time errors, doesn't mean there are no errors. Undefined behavior is a giant error.

TalShani
Jul 20, 2010, 09:22 AM
Also, just because there are no compile time errors, doesn't mean there are no errors. Undefined behavior is a giant error.
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?

dejo
Jul 20, 2010, 09:56 AM
Do you even understand the difference between @% and %@ ?

TalShani
Jul 20, 2010, 10:22 AM
Do you even understand the difference between @% and %@ ?

sure.. -.-

dejo
Jul 20, 2010, 10:35 AM
sure.. -.-
Alright. What is it then?

seepel
Jul 20, 2010, 11:46 AM
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?

So I'm confused, didn't you say that the app didn't crash, but that takeText.text was just an empty string? If the app crashes what is the error in the debugger? Are there any warnings when you compile? Often when I try to set an NSString to a constant and forget the @ symbol my app will crash without a peep.

Have you tried walking through your app with breakpoints? Are you SURE that this is the line causing your app to crash?

TalShani
Jul 20, 2010, 12:55 PM
So I'm confused, didn't you say that the app didn't crash, but that takeText.text was just an empty string? If the app crashes what is the error in the debugger? Are there any warnings when you compile? Often when I try to set an NSString to a constant and forget the @ symbol my app will crash without a peep.

Have you tried walking through your app with breakpoints? Are you SURE that this is the line causing your app to crash?

Yes, it shows an empty string.
And Yes, when I'm trying new commands for it the app crushes,
The only problem is this.

dejo
Jul 20, 2010, 02:03 PM
Yes, it shows an empty string.
You sure it contains an empty string, though? Maybe it's not showing what you think it should, since you don't seem to even understand the difference between %@ and @%.

And Yes, when I'm trying new commands for it the app crushes
What new commands are you trying? Don't expect us to guess. I suggest you give this blog post a good read through before you seek further help: Getting Answers (http://www.mikeash.com/getting_answers.html)

The only problem is this.
Doubtful.