I am having some issues in dealing with characters in NSStrings.
I read a XML file converted to NSData, it is okay, but when I transfer the "Element name" it has not converted to UTF-8.
I've tried here are some examples of the site, but with no success.
My Code is -
NSString * S = [NSString stringWithFormat: # "%#", D];
S = [S stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
Try this..
NSString *url = S;
NSString *check=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
check=[check stringByReplacingOccurrencesOfString:#"%0A" withString:#""];
Related
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.
I am doing this way. but i am not getting where is wrong.
NSString *strUrl = [NSString stringWithFormat:#"https://play.google.com/stor/apps/details?id=com.ShiftSharerfree_new&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5TaGlmdFNoYXJlcmZyZWVfbmV3Il"];
NSString *originalString = [NSString stringWithFormat:#"%#", strUrl];
NSData *data = [NSData dataFromBase64String:originalString];
NSString *data = [base64 originalString];
NSLog(#"data:%#", data);//[data base64EncodedString]);
Please guide me in above.
Perhaps you need a base64 conversion library, as mentioned here:
Convert between UIImage and Base64 string
I had a similar error with code for converting images to base64, using [base64 encode...] calls.
the first thing i'd noted is that the NSData and the NSString have the same name. Could this be the error?
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 am making request on server , having spaces in URL
http://xxxxxxxx.com/api/api.php?func=showAllwedsdwewsdsd¶ms[]=Saudi%20Arab¶ms[]=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¶ms5262721536=Saudi 0X0P+0rabia¶ms5 8288=All
I am also using
downloadURL= [downloadURL stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
but again strange url like
http://xxxxxxxxx.com/api/api.php?func=showsdsdsd¶ms[]=Saudi 0X1.240DC0D824P-807rabia¶ms[]=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¶ms[]=Saudi Arabia¶ms[]=%#",#"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¶ms[]=Saudi%20Arab¶ms[]=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¶ms[]=Saudi**%**%20Arab¶ms[]=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).
how can I have this conversion from NSWindowsCP1251StringEncoding to UTF-8?
I had several attempts but no one worked as it should. My last try was:
NSData *dt = [mystr dataUsingEncoding:NSUTF8StringEncoding];
NSString *str = [NSString alloc] initWithData:dt encoding:NSWindowsCP1251StringEncoding];
The result of str is unreadable. Did anyone encounter anything similar?
I think you were so close:
// Convert it back to CP1251
NSData *dt = [mystr dataUsingEncoding:NSWindowsCP1251StringEncoding];
// Now load it as UTF8
NSString *str = [NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];