Is there any function to remove the white spaces from text message in objective c?
For eg:for "How are you",the result should be "howareyou"
Thanks in advance.
You could use NSString's componentsSeparatedByCharactersInSet with whitespaceCharacterSet to first split the string on the whitespace, and then join the components using NSArray's componentsJoinedByString.
eg.
NSString *myString=#"How are you";
myString = [[myString componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceCharacterSet]] componentsJoinedByString: #""];
NSLog(myString); // displays Howareyou
Tom's approach isn't very efficient. What you want is:
-stringByTrimmingCharactersInSet:
NSString *myString = #"How are you";
myString = [myString stringByReplacingOccurrencesOfString:#" " withString:#""];
Related
i need to send smiley to other user through iphone app ,so i need to replace \ string with some unique string in obj c.
here if your string is #"\ud83d\ude04" then it is give error "Invalid Character" so put this ' special character and then use it ..
NSString *str = #"\'ud83d\'ude04";//// here if your string is #"\ud83d\ude04" then it is give error "Invalid Character" so put this ' special character and then use it
NSString *smileWithString = [str stringByReplacingOccurrencesOfString:#"\'" withString:#":)"];
[smileWithString retain];
NSLog(#"\n\n SmileString %# Str %#",smileWithString);
Update:
Here’s how to convert NSString to NSData – it’s really simple:
NSString *myString = #"Some String";
NSData *myData = [myString dataUsingEncoding:NSUTF8StringEncoding];
And what about the reverse conversion, i.e. how to convert NSData to NSString? Here’s one quick way:
NSString *myString = [NSString stringWithFormat:#"%.*s",[myData length], [myData bytes]];
Use encoding of NSString and when need to use or show string decode it.
Refer base64-encoding link.
Your looking for stringByReplacingOccurrencesOfString that should do the trick.
NSString *newString = [oldString stringByReplacingOccurrencesOfString:#"\" withString:#"uniqueString"];
I have NSString #" (\n "Bi_ss" \n) "
I want to get String #"Bi_ss "
Any one has any idea about this ?
Thank you
NSString *trimmedString = [string stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
From Cocoanetics...
Take a look at the NSString method stringByReplacingOccurrencesOfString:withString for example, this should be able to perform the basic task you want it to do.
I am building an iphone app that reads a phone number from the address book and dial it (with some other things of course...:)). When I load the phone numbers from the AB they are in this following format: "1 (111) 111-1111" which is not "dial-able" when using this:
fullNumber = [NSString stringWithFormat:#"%#%#", #"tel:", phoneNum];
Why is that happening? What would be the best way to approach this? How could I convert the phone number into a string of digits (with no spaces or "-")?
Thanks.
This will strip out all non-numeric characters:
phoneNumDecimalsOnly = [[phoneNum componentsSeparatedByCharactersInSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]] componentsJoinedByString:#""];
To answer your question about removing specific characters from a string... Take a look at the NSString Class Reference specifcally the method stringByReplacingOccurrencesOfString:withString:
You could do
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:#"(" withString:#""];
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:#")" withString:#""];
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:#"-" withString:#""];
fullNumer = [fullNumber stringByReplacingOccurrencesOfString:#" " withString:#""];
Obviously not the most efficient... but it should give you an idea of the method and how to strip out specific characters.
Another option that covers any character other than a number can be seen in the following SO post, this is likely a much better solution to your problem.
Remove all but numbers from NSString
The below code will allow only digits and + in the entered string.
phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:
[[NSCharacterSet characterSetWithCharactersInString:#"+0123456789"]
invertedSet]]
componentsJoinedByString:#""];
i am getting one url from the server http: / /www.google.com, however due to space in between them it is not opening, please let me know how to remove the space in between them, so that it should open in safari.
To remove empty space:
str = [str stringByReplacingOccurrencesOfString:#" " withString:#""];
Use following to detect site or phone number in text view.
textview.dataDetectorTypes = UIDataDetectorTypeAll;
NSString *cleanString = [dirtyString stringByReplacingOccurrencesOfString:#"http: //" withString:#"http://"];
I'd wonder why you get this odd string in the first place, though.
NSString *str = #"hel lo";
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
For remove empty spaces, please use below code,
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
I am new to Iphone development. I have tried the following code, but it does not work with middle whitespace in UITEXTFIELD.
NSString *t1 = [txt.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
How can I avoid middle whitespace?
Use this code may this use full.
NSString *t1=[txt.text stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *t1= [txt.text stringByReplacingOccurrencesOfString:#" " withString:#""]