Strings file Xcode - Remove the first and last " + ; - objective-c

I just started using strings file in my projects. Or well, i'm trying to.
I had an error for a few days, and finally figured out you actually have to make strings in the file with "" and end the lines with a ; - that was a bummer to figure out ;)
Anywhoo, now i'm trying to get it sorted out and separated into smaller strings. It's working like a charm, but i just can't seem to get rid of the "" and ; on each line of the string.
For instance, i have this in my strings file (i'm separating by comma):
"This is a string,This is another string,And a final one";
I am trying to get it into 3 different strings, and after having done it, it looks like this in my 3 strings objects:
"This is a string
This is another string
And a final one";
So i need to remove the " in string 1, and the " + ; in string 3.
I hope it made sense. I really can't seem to figure this one out, as i haven't really worked with sorting around in strings too much, and yet alone working with a strings file.
Thanks on advance.

This should do the trick if ALL strings have the " and "; at the beginning/end of the total string.
firstString = [firstString substringFromIndex:1];
lastString = [lastString substringToIndex:[lastString length] - 3];
If you want to remove all occurances of " and ; use this before splitting:
totalString = [totalString stringByReplacingOccurrencesOfString:#"\"" withString:#""];
totalString = [totalString stringByReplacingOccurrencesOfString:#";" withString:#""];

Typically, you use a tool to generate the .strings file from your code. You don't have to construct it manually.
In your code, when you want to use a string that should be localizable, you typically use one of the NSLocalizableString... macros. Like so:
textField.stringValue = NSLocalizableString(#"Hello, World!", "Standard programming greeting");
To generate the Localizable.strings file, you use the genstrings tool to scan your code for uses of such macros (and a few other things). It produces .strings files in the proper format.
For more, see these Apple documents:
Resource Programming Guide: String Resources
Internationalization Programming Topics

Related

Illegal characters in path (Chinese characters)

Getting an "Illegal characters in path" with Directory.GetFiles:
files = Directory.GetFiles(folderName & invoiceFile & "*.pdf")
Given the actual values, the filenames would be like so:
x:\folder1\請 010203.pdf
y:\foldera\folderb\請 040506.pdf
z:\xyz\abc\請 119906.pdf
Hence the * wildcard. Can I use Chinese characters with Directory.GetFiles? I think I can since I was able to use it on a separate VBA project before using ChrW(35531) so I think it shouldn't be a problem with .NET. Anyone know a fix for this?
You need to use Directory.GetFiles Method (String, String), like this:
files = Directory.GetFiles(folderName, invoiceFile & "*.pdf")
Note that the folder name and the filter are separate parameters.

Code for converting long string to pass in URL

I am trying to take a string like "Hello my name is Nick" and transform it to "Hello+my+name+is+Nick" to be passed through a URL. This would be easily done by replacing all the spaces with a + char however I also need to replace all special characters (. , ! &) with their ASCII values. I have searched the net but cannot find anything. I wonder if anyone knows of existing code to do this as its a fairly common task?
I think you're looking for this: HttpUtility.UrlEncode Method (String)
Handles non-URL compliant characters and spaces.

Outputting Formatted NSString

UIAlertView *message = [[UIAlertView alloc] initWithTitle:[[LanguageManager sharedLanguageManager] get:#"Notice"]
message:[NSString stringWithFormat:[[LanguageManager sharedLanguageManager] get:#"Notice_Text"]]
delegate:nil
cancelButtonTitle:[[LanguageManager sharedLanguageManager] get:#"Close"]
otherButtonTitles:nil];
Hi, let me explain my codes above. Basically it calls up an UIAlertView with data read from a .plist via my LanguageManager singleton class. The LanguageManager get function basically returns a NSString*. I know I should use the NSLocalizedString class but I had been using this class for a while now, so I had decided to stick to it.
My problem lies with the "message:" parameter. The string I am trying to read contains formatting characters like \n but it does not output correctly and appears as \n instead of a line break when printed. I also get the "Format string is not a string literal" warning. Other parts of the app using similar method to return a string which contains %d or %f works correctly though, just the '\n' character not working.
Does anyone have any idea how I may overcome this?
"\n" is not a "formatting character": the compiler translates it to the appropiate code; the string NEVER contains the "\" and "n" characters.
Thus, if you string comes from a source that is NOT compiled by a (Objective-)C(++) compiler, "\n" will be just the two characters. Nothing will turn them into a newline, unless you do it yourself with something like
NewString=[MyString stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"];
Note the two different strings: in the first case, "\" prevents the compiler from doing the \n -> newline conversion, while the second string will be an actual newline.
The warning about a non-literal format string is somewhat pointless; I've yet to find a good way to get rid of that one (for now, I just disable it entirely, using -Wno-format-nonliteral on clang++ >= 4.0).

Tool to format lines of text into array

I frequently come across this problem. I have a file:
something
something2
something3
which I want output as:
"something","something2","something3"
any quick tool for this, preferably online?
If its just a one off thing, it'd be pretty easy to just do it with a search & replace in any advanced-ish text editor...
For example in notepad++:
Do a Replace (CTRL+H)
Set "Search Mode" to "Extended"
Find: \r\n
Replace with: ","
(of course you'll need an extra quote at the very start & very end of the file).
If you need to do it more than once, writing a small script/program that did a regular expression replace over the file would be fairly straight forward too.
Edit: If you really wanted to do it online, you could use an online regular expression tester (in this case you want to use \n as the regex and "," as your replace pattern, leaving the other settings alone).
A quick Python hack?
lines = open('input.txt').xreadlines()
txt = ','.join(['"%s"' % x for x in lines])
open('output.txt', 'w').write(txt)

componentsSeparatedByString for SFV files

I'm having an NSString like this:
music that rocks.mp3 e99a65fb
The problem is that between the filename (musicthatrocks.mp3) and the CRC32 checksum (e99a65fb) there could be many more spaces then one. How can I split the line into an array?
I thought of using componentsSeparatedByString, but my problem is that the filename can also contain spaces.
Thanks in advance.
If you're OK with regular expressions, you could do it that way. For example (using RegexKitLite):
NSString * fileName = [line stringByMatching:#"(.*)\\s" capture:1];
An explanation of the regex: (.*) This will match as many characters as it can, until it finds a space character. However, the capture is greedy, which means it's going to grab as many characters as it can before the last space character (in a nutshell).
Or you can use NSString methods to find the last occurrence of the space character and get a substring from the beginning of the line to the last space character.
Or you can split the string base on #" ", throw away the last object in the array, and then recombine the array with #" ".
Use componentsSeparatedByString:.
Get the lastObject, which is the CRC32.
Use subarrayWithRange: to create a new array without the CRC32.
Use componentsJoinedByString: to reconstitute the filename.
You may want to do step 4 in a loop, repeatedly deleting the last object until either the filename exists in the target directory or you run out of empty strings at the end of the array. This handles the case of multiple spaces between filename and CRC32, as well as the case of a filename ending in spaces (pathological but possible).