How to determine if an NSString is latin based? - iphone

I'm trying to determine if a string is latin based or Japanese.
I've tried something like the following but it returns YES for Japanese strings as well:
NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
BOOL isAlpha = [[myStr stringByTrimmingCharactersInSet:alphaSet] isEqualToString:#""];
A string might be a word like "café" or something like "カフェ" or "喫茶店".

Use the canBeConvertedToEncoding: method. For example:
BOOL isLatin = [myString canBeConvertedToEncoding:NSISOLatin1StringEncoding];
Available encodings are here.

Related

Convert or Print CGPDFStringRef string

How to convert a CGPDFStringRef to unicode char? I have used CGPDFStringCopyTextString to get the string and then [string characterAtIndex:i] to cast to unichar, is this the right way? or is there any way to get the bytes of the string and convert to unicode directly?
Need some guidance here.
NSString is capable of handling of unicode characters itself, you just need to convert the CGPDFString to NSString and further you can use it as follows:
NSString *tempStr = (NSString *)CGPDFStringCopyTextString(objectString);
although UPT's answer is correct, it will produce a memory leak
from the documentation:
CGPDFStringCopyTextString
"...You are responsible for releasing this object."
the correct way to do this would be:
CFStringRef _res = CGPDFStringCopyTextString(pdfString);
NSString *result = [NSString stringWithString:(__bridge NSString *)_res];
CFRelease(_res);
It's not a bad idea, even if you can access the CGPDFString directly using CGPDFStringGetBytePtr. You will also need CGPDFStringGetLength to get the string length, as it may not be null-terminated.
See the documentation for more info

Different kind of UTF8 decoding in NSString

I have searched a lot about UTF8 decoding, but not found the answer yet.
I receive an UTF-8 decode NSString from my NSXMLParser:
NSString *tempString = #"Test message readability is óké";
In someway I can't find the way to change this encoded text to:
Test message readability is óké
I could tell all the options I tried but I don't think that should be necessary. Could please some help?
Thnx!
The NSXMLParser will treat the text using the character encoding that the XML specifies. I believe in your case the XML do not specify UTF-8 explicitly.
The text seems to be ISO Latin 1. If you can not do anything about the server generating the XML then you can apply this hack:
char* tempString = [string cStringUsingEncoding:NSISOLatin1StringEncoding];
string = [NSString stringWithUTF8String:tempString];
I have verified that this works by testing this from the GDB prompt:
po [NSString stringWithUTF8String:(char*)[#"Test message readability is óké" cStringUsingEncoding:5]]
You're doing it wrong. What you want is:
char *s = "Test message readability is óké";
//Note: this is a one-byte-character C string, not an NSString!
NSString *tempString = [NSString stringWithCString:s encoding:NSUTF8StringEncoding];
Also keep in mind that when you initialize string constants, what actually goes to program memory depends on the encoding of the current file. If it's already UTF-8, then the characters will be doubly-encoded - you'll get characters Ã,³, etc. encoded as UTF8 in the C string.
In other words, using a string constant is probably a wrong move to begin with. Please give more context to the problem.
Standart encoding and decoding like this:
For encoding:
NSString *content = [bodyTextView.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
For decoding:
NSString *decodedString = [msg.content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

How can I remove quotes from an NSString?

I am trying to remove quotes from something like:
"Hello"
so that the string is just:
Hello
Check out Apple's docs:
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/
You probably want:
stringByReplacingOccurrencesOfString:withString:
Returns a new string in which all occurrences of a target string in the receiver are replaced by another given string.
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
So, something like this should work:
newString = [myString stringByReplacingOccurrencesOfString:#"\"" withString:#""];
I only wanted to remove the first quote and the last quote, not the quotes within the string so here's what I did:
challengeKey = #"\"I want to \"remove\" the quotes.\"";
challengeKey = [challengeKey substringFromIndex:1];
challengeKey = [challengeKey substringToIndex:[challengeKey length] - 1];
Hope this helps others looking for the same thing. NSLog and you'll get this output:
I want to "remove" the quotes.

Ignore case UITextField/UITextView

I would like to ignore all case sensitivity to be ignored for my UITextField and UITextView. I would also like to know how to detect if the string is upperCase or lowerCase. Thanks.
[myString uppercaseString] will give you the myString variable transformed to use only uppercase letters, [myString lowercaseString] will give you the lowercase version, respectively.
You can use it to check for uppercase (lowercase) strings like this:
if ([[textField text] isEqualToString:[[textField text] uppercaseString]]) {
NSLog("String is uppercase!");
}
If you have a reference string and want to compare it ignoring the case, you can just use caseInsensitiveCompare: method of NSString:
[referenceString caseInsensitiveCompare:[textField text]];

How to get the string value from a string which contains ","?

I have a string value where it contains comma. Ex:- 1,234. I want to get the value of the string where i need only 1234. Can you please help me...
Instead of manually stripping out the commas, it might be more elegant (and less error-prone if you support different locales) to use an NSNumberFormatter to convert the string to a number.
NSString *myString = "1,234";
NSString *resultString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
If you want to strip the comma then: -
NSString *string = #"1,234";
string = [string stringByReplacingOccurrencesOfString:#"," withString:#""];
This should return you a string with just 1234 in it.
By 'getting the value' do you mean, converting this to a NSNumber object? If so use this
NSNumber *numberFromString = [NSNumber numberWithInteger:[string integerValue]];
I don't know that framework/language, but if an integer converter won't work, then strip out the commas by replacing them with null from the string and then convert to an integer.