displaying german characters in iphone - iphone

i have following string coming in the json response.
"Gas-Heizung-Sanit\u00e4r" so how to display it. i want to display that \u00e4 as a german character..
NSString *str = "Gas-Heizung-Sanit\u00e4r";
NSLog(#"%c",str);
it only prints the german character.

the following works
NSString *str = #"Gas-Heizung-Sanit\u00e4r";
NSLog(#"%#",str);
You forgot the # on the string and also the %c is for a character, you should use the %# for a string

Related

remove specific characters from NSString

I wants to remove specific characters or group substring from NSString.
mean
NSString *str = #" hello I am #39;doing Parsing So $#39;I get many symbols in &my response";
I wants remove #39; and $#39; and & (Mostly these three strings comes in response)
output should be : hello I am doing Parsing So i get many symbols in my response
Side Question : I can't write & #39; without space here, because it converted in ' <-- this symbol. so i use $ in place of & in my question.
you should use [str stringByReplacingOccurrencesOfString:#"#39" withString:#""]
or you need replace strings of concrete format like "#number"?
try below code ,i think you got whatever you want simply change the charecterset,
NSString *string = #"hello I am #39;doing Parsing So $#39;I get many symbols in &my response";
NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:#"#39;$&"];
NSString *result = [[string componentsSeparatedByCharactersInSet:trim] componentsJoinedByString:#""];
NSLog(#"%#", result);

Junk character in Webview

I am getting html content as a string in my webservice response which contains "&nbsp" in it. When I display that data in webview, "&nbsp" is converted in junk character. Please let me know how to solve it.
You have to read the string as NSUTF8 encoded string and then pass the string to web view using "loadHTML" method mentioned in UIWebView.
Not only that, if you want to display special characters like copy right, double quotes etc or other language characters in the HTML, you have to use UTF8 encoding.
Use the stringByReplacingPercentEscapesUsingEncoding: method of NSString
like :
NSString *decoded = [yourString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Also to remove use
[yourString stringByReplacingOccurrencesOfString:#" " withString:#" "];
stringByReplacingOccurrencesOfString is deprecated from ios 9
let decoded = yourString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

iPhone: Dynamic spaces in NSString

It may be a simple question, but i could't get the answer and needing your help!
I have a string like,
NSString *temp = #"Hello How are you?";
I have to provide spaces dynamically starting in this string by code. For ex: I need to dynamically add 5 spaces in this string in starting point. So, the output string will be like,
#" Hello how are you?"
My doubt is, how can i add spaces dynamically to a existing string? I need it to do this way only, not via any other way like string concatenation etc. due to my requirement.
So, please advise me how can i add spaces dynamically in starting point of the existing string.
Note: The spaces will vary every time, its not constant that i can provide 5 spaces only, it will vary.
Thank you!
An NSString is immutable, so you have to create a new string in any case.
The following code will create a front-padded string with padLength spaces:
int padLength = 10;
NSString* originalString = #"original";
NSString* leadingSpaces = [#"" stringByPaddingToLength:padLength];
NSString* resultString = [NSString stringWithFormat:#"%#%#", leadingSpaces, originalString];

iPhone - Comparing strings with a German umlaut

I've few German strings (with umlauts like åä etc) in NSArray.
For example consider a word like "gënder" is there in array.
User enters "gen" in a text field.
I can to check the words in string that matches the characters "gen".
How can I compare the string by consider umlauts as english strings...?
So in above example, when user enters "gen", it has to return "gënder".
Is there any solution for this type of comparision?
Use the NSDiacriticInsensitiveSearch option of the various NSString compare methods. As described in the documentation:
Search ignores diacritic marks.
For example, ‘ö’ is equal to ‘o’.
For example:
NSString *text = #"gënder";
NSString *searchString = #"ender";
NSRange rng = [text rangeOfString:searchString
options:NSDiacriticInsensitiveSearch];
if (rng.location != NSNotFound)
{
NSLog(#"Match at %#", NSStringFromRange(rng));
}
else
{
NSLog(#"No match");
}

StringByAddingPercentEscapes not working on ampersands, question marks etc

I'm sending a request from my iphone-application, where some of the arguments are text that the user can enter into textboxes. Therefore, I need to HTML-encode them.
Here's the problem I'm running into:
NSLog(#"%#", testText); // Test & ?
testText = [testText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"%#", testText); // Test%20&%20?
As you can see, only the spaces are encoded, making the server disregard everything past the ampersand for the argument.
Is this the advertised behaviour of stringByAddingPercentEscapes? Do I have to manually replace every special character with corresponding hex code?
Thankful for any contributions.
They are not encoded because they are valid URL characters.
The documentation for stringByAddingPercentEscapesUsingEncoding: says
See CFURLCreateStringByAddingPercentEscapes for more complex transformations.
I encode my query string parameters using the following method (added to a NSString category)
- (NSString *)urlEncodedString {
CFStringRef buffer = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)self,
NULL,
CFSTR("!*'();:#&=+$,/?%#[]"),
kCFStringEncodingUTF8);
NSString *result = [NSString stringWithString:(NSString *)buffer];
CFRelease(buffer);
return result;
}