I want to insert a unicode symbol for pi, which is \u03c0 into a label and for it to display the symbol. I am loading this in from an array which was read from a txt file. For example if I have a txt file that contains "\u03c0":
string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]
array[i] = string;
label.text = array[i];
What am getting is "\u03c0" as an output in the textfield, but I want the symbol. What I am doing wrong?
Edit: it seems that my problems is with string encoding because I am reading in the array from a file. I was using NSUTF8StringEncoding. What should this be changed to to allow unicode?
My guess is the contents of your file contains \\u03c0 rather than the actual character. If you have control of the file contents, paste in the actual character, not the sequence, because the editor will save it with the escaping "\". If you don't have control, i suggest writing code to detect this escaping, strip the preceding "\" and then use the result in your format.
Related
I have a DataFile, built by subsidiairy Application. I need to locate some substring contained in the data file. They are identifiable by the character symbols delimiting them. For instance : *!substringqSxt .The substring will vary from a project to another so I need to locate the symbols delimiting them to read the following substring. I also printed the file to different encodings trying which one was used and matched the original data file. found it was MacOsRomanStringEncoding.
I use NSRange:rangOfStringto locate the delimiting symbols. Here is my code :
char *debutAudio ="jjbj";
char *finAudio ="qSxt";
NSString *debutAudioConverted = [[NSString alloc]
initWithCString: debutAudio
encoding:NSMacOSRomanStringEncoding];
NSString *finAudioConverted = [[NSString alloc]
initWithCString: finAudio
encoding:NSMacOSRomanStringEncoding];
NSRange debutaudioRange =[dataFileContent rangeOfString:debutAudioConverted];
NSRange finaudioRange =[dataFileContent rangeOfString:finAudioConverted];
NSLog(#"range is %#",NSStringFromRange(debutaudioRange));
NSLog(#"range is %#",NSStringFromRange(finaudioRange));
Both NSLog returns range is {9223372036854775807, 0}
so not locating the delimiting strings there.
And if I ask to look for other strings contained in the file like "Settings" the rangeOfString will return the proper location and length.
I thought the file may contain multiple encodings, and tried converting with initWithCStringto any possible encoding but nothing would do.
Also if I open the file in text edit and use the "Find" function, it will not locate the delimiting string, but will locate other words. My guts tell me its related. I dont know where to look for info. Could the file be protected, I am reading a copy of it though.
I have found the problem occuring here. The proper encoding is still MacOsRoman. The problem is the prefix string *debutAudio "jjbj"there is actually a tiny space , like a quarter space between each characters. I have tried every unicode spaces listed here :https://www.cs.tut.fi/~jkorpela/chars/spaces.html#adj
without any success. Now I will tried to find some half or quarter space under MacOsRoman see if that is working.
Basically i have a concern, which needs to be solved using Objective C alone. (i have tried with C)
Is there any appropriate way in objective C to read character by character (till EOF) from a file, The file is got from document directory or bundle.
Problem : So, If i want to append a escape character () before all inverted comma's in a file and A special character (say /) before each line.
Replace
(type == "Currency")
with
/(type == \"Currency\")
Thanks in advance.
Try this may be this helps you.
I assume your file contains only string data not JSON or XML
NSString *strFileContent = [NSString stringWithContentsOfFile:#"pathofyourfile" encoding:NSUTF8StringEncoding error:nil];
NSLog(#"strFileContent %#",strFileContent);
NSString *newStr = [strFileContent stringByReplacingOccurrencesOfString:#"(" withString:#"/("];
Like last line you can replace your data.
Hope this works for you.
I'm using the following to define an NSString containing the Tab character:
#define TAB #"\t"
I'm writing a longer NSString to a file using writeToFile:atomically:encoding:error, using encoding: NSUTF8StringEncoding. This longer NSString contains TAB characters.
When I open the resulting file in TextEdit, I see a character that looks like a Japanese glyph in the place of the TAB character. Here is a screen shot of a line that is intended to have two tab characters, but which has these odd characters instead:
odd characters http://www.market-research-services.com/starpowermedia/for_distribution/tab-char-anomaly.png
What is the correct way to #define an NSString that will contain a TAB character to be written to a file of NSUTF8StringEncoding?
Thanks in advance to all for any info.
I am new in programming. I have string NSString *string = #"\U0420\U043e\U0437\U044b"; and after each slash('\') i need put another slash to get string like this #"\\U0420\\U043e\\U0437\\U044b"
I am new to programming and objective-c. please help.
My original answer was:
Use [NSString stringByReplacingOccurrencesOfString:withString:] (reference).
NSString *string = #"\U0420\U043e\U0437\U044b";
NSString *converted = [string stringByReplacingOccurrencesOfString:#"\\"
withString:#"\\\\\\"];
However I now don't think that's right given the \ characters won't actually exist in string; instead the compiler will convert each of those sequences into a unicode character. You will need to encode string as this:
NSString *string = #"\\U0420\\U043e\\U0437\\U044b";
In order to use the above code. I cannot see any alternative to this.
Further Update: Often when I've come across questions like this there is a confusion between string literals and string data. In your question those \ characters won't appear as the compiler will have converted them into unicode characters (\Uxxx is a unicode escape sequence for a single character). However if you provided a string like that at runtime (say read from a text file) then those \ characters will exist and you can use the code above.
Does anyone know if this line of code would work for a NSString from an rtf file on iOS?
NSString* cList = [[NSString alloc] initWithContentsOfFile:#"name of file" encoding:NSUTF8StringEncoding error:nil];
c = [cList componentsSeparatedByString:#"\n• "];
I'm just wondering since it includes a bullet point character which I pretty much copy pasted. I wasn't expecting it to be that easy. It just seems like it should be an escape sequence character or something.
Probably should've included some form of error checking in the first line, but that aside for the moment.
Update: After much compiling with no success with an rtf, I copied the text into a txt and used that instead. Works the first time. Seemed like the rtf reading was getting weird rtf data that wasn't really what I was after when I tried to NSLog it.
Thanks!
How about using unicode sequence?
like...
c = [cList componentsSeparatedByString:#"\n\u0000 "];
0000 <---- unicode.