How to remove empty space from NSString in iPhone - iphone

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"];

Related

How to replace more than one common sub string in a single string with another string?

for(int i= 0 ;i<[urlsArrray count]; i++)
{
NSString *urlString = [urlsArrray objectAtIndex:i];
NSString *escapedUrlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:escapedUrlString];
NSString *urlstring1 = [url absoluteString];
NSArray *parts = [urlstring1 componentsSeparatedByString:#"/"];
NSString *fileName = [parts objectAtIndex:[parts count]-1];
NSMutableString *tempString = [NSMutableString stringWithString:fileName];
// [tempString replaceCharactersInRange:[tempString rangeOfString:#"%20"] withString:#" "];
NSLog(#"file name in temp string: %# word name: %#", tempString, wordNameDB);
NSRange match = [tempString rangeOfString:wordNameDB];
if(match.location != NSNotFound)
{
NSLog(#"match found at %u", match.location);
isAvailable = YES;
break;
}
Hi friends, now my problem is i am getting file name from server..., if file name is having any spaces then it replace '%20' ( i.e ex: "hello world" is actual name but i am getting file name like: "hello%20world") .
1. I am not sure all file names having spaces.
2. And also i am not sure a file may have only one space
so first i have to check the file is having spaces or not, if have then i want to replace all "%20" with #" " string. Please give me any suggestions or code snippets.
OR " THERE IA ANY OTHER WAY TO READ FILE NAMES WITHOUT GETTING '%20' IN THE PLACE OF SPACE(#" ")..... thank you
If you have your file name stored in fileName param, you can use the following:
fileName = [fileName stringByReplacingOccurrencesOfString:#"%20" withString:#" "];
The above code will replace all "%20" with " ". If there are no "%20" in the fileName, you will get back the same string.
Correction:
I was confused with stringByAddingPercentEscapesUsingEncoding mentioned in code and thought you have already used stringByReplacingPercentEscapesUsingEncoding. If you are not using stringByReplacingPercentEscapesUsingEncoding method, you should use that in this case. The above code is useful, only if that is not able to remove any particular string which you want to replace.
What you need is replacing the escape charcters, according to the encoding.
Use this and all your spaces and other URL encoded characters will be converted to what you need.
[#"yourString" stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
THERE IA ANY OTHER WAY TO READ FILE NAMES WITHOUT GETTING '%20' IN THE PLACE OF SPACE(#" ")
Yes, use this:
NSString *newString = [yourstring stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Use this to remove spaces ..
urlString = [urlString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
You seem to already have a valid NSURL object representing the file. Getting the filename from a URL is easy:
...
NSURL *url = [NSURL URLWithString:escapedUrlString];
NSString *path = [url path];
NSString *filename = [path lastPathComponent];
No fiddling with unescaping percent escapes, URL parsing, and other error prone stuff.

Bad URL when creating NSURL

I am making request on server , having spaces in URL
http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd&params[]=Saudi%20Arab&params[]=all
I was getting the error Bad URL so ,
I used
downloadURL = [downloadURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
but due this I am getting strange URL as
http://sdsdsdsdsdsd.com/api/api.php?func=showAllsdsd&params5262721536=Saudi 0X0P+0rabia&params5 8288=All
I am also using
downloadURL= [downloadURL stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
but again strange url like
http://xxxxxxxxx.com/api/api.php?func=showsdsdsd&params[]=Saudi 0X1.240DC0D824P-807rabia&params[]=All
Please help how can I solve this issue
Edit Pasting Big portion of code
PropertyXMLDownloader *download=[[PropertyXMLDownloader alloc]init];
[download setDelegate:self];
firstAppDelegate *del=(firstAppDelegate *)[[UIApplication sharedApplication]delegate];
NSLog(#"Country is %#",del.country);
NSLog(#"State is %#",del.state);
// del.country=[del.country stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *downloadURL=[NSString stringWithFormat:#"http://xxxxxxxx.com/api/api.php?func=showAll&params[]=Saudi Arabia&params[]=%#",#"all"];
// downloadURL= [downloadURL stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
//downloadURL = [downloadURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#" "];
downloadURL = [[downloadURL componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString:#"%20"];
NSLog(downloadURL);
[download startDownloading:downloadURL];
try this.
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:#" "];
downloadURL = [[downloadURL componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString:#"%20"];
Perhaps the %20 is being seen as a data argument, like %# or %g. Try defining the NSString using
NSString *urlString = [NSString stringWithFormat:#"http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd&params[]=Saudi%20Arab&params[]=all"];
and you'll see a warning. 'Escaping' the percent sign by adding another in front of it:
NSString *urlString = [NSString stringWithFormat:#"http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd&params[]=Saudi**%**%20Arab&params[]=all"];
and the warning goes away.
Your problem is this:
NSLog(downloadURL);
Try replacing it by:
NSLog(#"%#", downloadURL);
and everything will work.
You can use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding and forget about all the workarounds.
(Explanation: since downloadURL contains % signs, it will not play well with NSLog, which expect a format string as its first argument, where % signs identify placeholders to be replaced).

Convert an iPhone phone number format for a "dial-able" string

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:#""];

avoid middle whitespace in UITextField

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:#""]

Truncate white space in text in iphone/objective c

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:#""];