issue in encoding a string with percent escaping iphone - iphone

This is my string.
NSString *str=#"A & B";
now i am converting it to NSUTF8StringEncoding.
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"str: %#", str);
space is replaced with %20 but & is not replaced with %26.
nslog show
str: A%20&%20B
This is also not working
NSString *str=#"(A) & (B)";
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"str: %#", str);
nslog show
str: (A)%20&%20(B)
I need this because i have to pass this as parameter value in webservice.
Anybody have idea for this. Please help me for this issue. Nice answer will be appreciated

You can use CFURLCreateStringByAddingPercentEscapes() to do that:
NSString *string = ...;
NSString *encodedString = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR("!*'();:#&=+$,/?%#[]"), kCFStringEncodingUTF8));

Related

Converting UTF8 Hex string to regular UTF8 encoded NSString

I am getting UTF-8 (hex): Hc3b8rt back from a server instead of the string "Hørt".
I need to convert this response to regular UTF-8.
What I have tried:
NSString *string = [dict objectForKey:#"suggest"];
const char *cfilename=[string UTF8String];
NSString *str = [NSString stringWithUTF8String:cfilename];
Thank you for your time!
There's no way you can decode this. As #JoachimIsaksson stated in the comments above, how can you tell if "abba" is exactly "abba" or two unicode chars?
use string encoding, NSISOLatin1StringEncoding
- (id)initWithCString:(const char *)nullTerminatedCString
encoding:(NSStringEncoding)encoding
Or shortly,
NSString *str = [NSString stringWithCString:cfilename
encoding:NSISOLatin1StringEncoding];
Edit after comments:
This is kind of strange. I have done some experiments after your comments and found some strange behaviour.
- (void) testStringEncodingOK {
NSString *string = #"h\u00c3\u00a5r";
const char *cfilename=[string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *cs = [NSString stringWithUTF8String:cfilename];
NSLog(#"String: %#", cs);
}
This output: hår
But if you get the \U in capital, not \u, then I replaced them to \u. And then it did not work. Seem the ,
- (void) testStringEncodingConfused {
NSString *string = #"h\\U00c3\\U00a5r";
string = [string stringByReplacingOccurrencesOfString:#"\\U" withString:#"\\u"];
NSLog(#"Original string:%#", string); // now string = #"h\u00c3\u00a5r"
const char *cfilename=[string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *cs = [NSString stringWithUTF8String:cfilename];
NSLog(#"String: %#", cs);
}
The output is, h\u00c3\u00a5r
Use below code..
const char *ch = [yourstring cStringUsingEncoding:NSISOLatin1StringEncoding];
 yourstring = [[NSString alloc]initWithCString:ch encoding:NSUTF8StringEncoding];
NSLog(#"%#",yourstring);
let me know it is working or not...
Happy Coding....
use this code
NSString *string = [dict objectForKey:#"suggest"];
const char *cfilename=[string stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *str = [NSString stringWithUTF8String:cfilename];
and tell if it is working or not.

how to use stringByTrimmingCharactersInSet in NSString

I have a string which gives the Date (below)
NSString*str1=[objDict objectForKey:#"date"];
NSLog(#" str values2%#",str1); --> 04-Jan-13
Now Problem is I need to Trim the"-13" from here .I know about NSDateFormatter to format date.but I can't do that here.I need to trim that
For that I am using:-
NSCharacterSet *charc=[NSCharacterSet characterSetWithCharactersInString:#"-13"];
[str1 stringByTrimmingCharactersInSet:charc];
But this does not work.this does not trim...how to do that..help
Not sure why not use an NSDateFormatter but here's a very specific way to approach this (very bad coding practice in my opinion):
NSString *theDate = str1;
NSArray *components = [theDate componentsSeparatedByString:#"-"];
NSString *trimmedDate = [NSString stringWithFormat:#"%#-%#",[components objectAtIndex:0],[components objectAtIndex:1]];
But this does not work.this does not trim...
It does trim, but since NSString is immutable, the trimmed string is thrown away, because you do not assign it to anything.
This would work (but do not do it like that!)
str1 = [str1 stringByTrimmingCharactersInSet:charc];
What you do is not trimming, it's taking a substring. NSString provides a much better method for that:
str1 = [str1 substringToIndex:6]; // Take the initial 6 characters
Something like this:
NSString *trimmed = [textStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
or this:
NSString *trimmed = [textStr stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"-13"];
what you have done is correct. Only thing is stringByTrimmingCharactersInSet returns NSString. So you need to assign this value to NSString, like
str1 = [str1 stringByTrimmingCharactersInSet:charc];
if you're sure you have your string always formatted like "NN-CCC-NN" you can just trim the first 6 chars:
NSString* stringToTrim = #"04-Jan-13";
NSString* trimmedString = [stringToTrim substringToIndex:6];
NSLog(#"trimmedString: %#", trimmedString); // -> trimmedString: 04-Jan

xcode - decode xml output string

NSString *col1 = [aBook.name stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#" \n\t"]];
NSLog(#"column1: %#", col1); //output:- ABC+Company
This is my xml data output.
It comes with '+' mark. How can i decode this?
Could you please help me?
You mean get the parts that are seperated by the '+' sign? You can do that with:
NSArray *stringArray = [col1 componentsSeparatedByString:#"+"];
[stringArray objectAtIndex:0]; //ABC
[stringArray objectAtIndex:1]; //Company
If you want to remove/replace the '+' sign you can do this:
NSString* string = [col1 stringByReplacingOccurrencesOfString:#"+" withString:#""]; //last string can be empty or have a string value
Hope this helped you!
You can also replace the plus signs with spaces, if that’s what you’re after:
NSString *name = [col1 stringByReplacingOccurrencesOfString:#"+" withString:#" "];

ObjC: how to add escape character so "hello" becomes \"hello\"?

Currently I am using stringByReplacingOccuranceOfString... to replace " with \", but is there any smarter way?
Thanks!
You can do like this,
NSString *str = #"hello";
NSString *escape = #"\\\””; // this adds ***//*** & then ***/"***
NSString *newStr = [NSString stringWithFormat:#"%#%#%#", escape, str, escape];
NSLog(#"%#", newStr);

How to replace a character in NSString without inserting a space?

Let's assume I have the string
NSString* myString = #"Hello,";
How can I remove the comma without leaving a space? I have tried:
NSString* newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
and
NSString* newString = [myString stringByTrimmingCharactersInSet:[NSCharacterSet punctuationCharacterSet]];
But both are leaving spaces.
I just ran the following as a test
NSString * myString = #"Hello,";
NSString * newString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
NSLog(#"%#xx",newString);
And I get 2010-04-05 18:51:18.885 TestString[6823:a0f] Helloxx as output. There is no space left.
NSString *newString = [myString substringToIndex:5];
That will ensure that there are only 5 characters in the string, but beware that this will also throw an exception if there are not at least 5 characters in the string to begin with. How are you handling this string? Is it being displayed to the user? Is it being written to a file? The code that you posted does not reproduce the error, so perhaps you should post the code that you are having a problem with.
the other way u can use.. by stringByReplacingCharactersInRange
token = [token stringByReplacingCharactersInRange:NSMakeRange(i, 1) withString:#"*"];
"token"is your want to replace the NSString and "i" is you want to change NSString by "*"