Keep text the same while keeping special characters - objective-c

i have a text that contains end of lines; i would like to have that text introduced into a NSString and still recognize the end of line.
i.e. i don't want to have to place a "\n" at the end of every line.
how can i do so in Obj-c?

I think it's best to hold the text using an NSArray, each element of which is a separate line. You can use [NSString componentsSeparatedByCharactersInSet:] (reference) for that:
NSString *str = #"hello\nworld";
NSArray *lines = [str componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

Related

How to break a NSString into words with non-significant blank suppression?

I have several NSStrings with a format similar to the one below:
"Hello, how are you?"
How can I break the string into an array of words? For example, for the above sentence I would expect an array consisting of "Hello,", "how", "are", "you?"
Usually I would break the string into words by using the function [NSString componentsSeparatedByCharactersInSet: NSCharacterSet set]
However this won't work in this situation because the spaces between the words are of unequal length. Note I will not be aware of the size of each word and the space between them.
How can I accomplish this? I am working on an app for OSX not iOS.
EDIT: My eventual goal is to retrieve the second word in the sentence. If there is a easier way to do this without breaking the string into an array please feel free to suggest it.
Try this:
NSMutableArray *parts = [NSMutableArray arrayWithArray:[str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
[parts removeObjectIdenticalTo:#""];
NSString *res = [parts objectAtIndex:1]; // The second string
Well, you could actually write a loop to iterate through the characters and find the first non-blank after the first blank, then iterate further to find the ending blank (or end of line). Would probably be about 5x faster (with much fewer object allocations) than using one of the other methods, and could be done in about 10 lines.
If you dont want to use a CharacterSet try this to remove extra spaces:
NSString* string = #"word1, word2 word3 word4";
bool done = false;
do {
NSString tempStr = [string stringByReplacingOccurrencesOfString:#" " withString:#" "];
done = [string isEqualToString:tempStr];
string = tempStr;
} while (!done);
NSLog(#"%#", string);
this will output "word1, word2 word3 word4"

Parser over txt document in xcode

I would like to go through .txt document and store text blocks in NSStrings. My problem is that this document contains linebreaks, and i don't know how to rid of those. It would be nice, if i could put each individual word into an ordered NSArray and then just go through that array and get information out from that. I would something like this:
// txt file
This is just a test.
End of the text file.
// NSArray and NSStrings
NSArray *wholeDocument =#"This","is","just","a","test","Foo","bar.", "End", "of", "the","text","file.";
NSString *beginDocument =#"This is just a test";
NSString *endDocument =#"End of the text file.";
Try this:
NSString *str = [NSString stringWithContentsOfFile:#"file.txt"];
NSArray *arr = [str componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Here, arr will contain all the words which are separated either by spaces or by line breaks.

Why is it that every other object in my array is blank?

I read a CSV file into an array using:
NSString *theWholeTable = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"example" ofType:#"csv"]
encoding:NSUTF8StringEncoding
error:NULL];
NSArray *tableRows = [theWholeTable componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
And every second object in the array is empty, any idea why?
the CSV file data looks like this:
10,156,326,614,1261,1890,3639,5800,10253,20914
20,107,224,422,867,1299,2501,3986,7047,14374
with 10 and 20 being the start of each new line.
thanks in advance.
Edit
I tried using the following code instead:
NSArray *tableRows = [theWholeTable componentsSeparatedByString:#"\n"];
And that worked the way I wanted it too.
Although I am still unsure why the newlineCharacterSet created empty objects...
If your CSV file comes from a non-UNIX system, it may contain multiple line separators (e.g. \r\n instead of \n). In this case, componentsSeparatedByCharactersInSet will insert empty strings for empty character sequences between \r and \n.
You can remove empty strings from NSArray using this method:
tableRows = [tableRows filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"length > 0"]];
to solve this problem, I have used other way:
NSArray *tableRows = [theWholeTable componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"\n"]]
So you wouldn't need remove any lines.

Replacing list of strings in a string (Objective-c 2.0)

For example I'd like to replace all occurrences of #"a" and #"b" in #"abcdabcd" with #"z". I'm currently doing this with repeated called to stringByReplacingOccurencesOfString:withString::
NSString *s1 = #"abcdabcd";
NSString *s2 = [[s1 stringByReplacingOccurencesOfString:#"a" withString:#"z"]
stringByReplacingOccurencesOfString:#"b" withString:#"z"];
What's a better way? I didn't find any similar methods that take an array of strings to replace.
You can use regular expressions:
NSString *s2 =
[s1 stringByReplacingOccurrencesOfString:#"[ab]"
withString:#"z"
options:NSRegularExpressionSearch
range:NSMakeRange(0, s1.length)];
There's also NSMutableString's replaceOccurrencesOfString:withString:options:range: method (so you don't have to create a new NSString object for every replacement call you want to make). Documentation linked for you.

\n does not skip to next line in NSString

NSMutableString *a = #"Hi";
NSMutableString *b =[a stringByAppendingString:#"\n\n Hi Again"];
The above doesn't give an error but does not put "Hi Again" on the next line. Why?
EDIT2
I realised after posting, that the OP had NSString in the title but put NSMutableString in the code. I have submitted an edit to change the NSMutableString to NSString.
I will leave this as it still maybe helpful.
Well I am surprised that does not give an error, because you are giving a NSMutableString a NSString.
You need to read the Documentation on NSMutableStrings.
to give you an idea
//non mutable strings
NSString *shortGreetingString = #"Hi";
NSString *longGreetingString = #"Hi Again";
/*mutable string - is created and given a character capacity The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string
*/
NSMutableString *mutableString= [NSMutableString stringWithCapacity:15];
/*The mutableString, now uses an appendFormat to construct the string
each %# in the Parameters for the appendFormat is a place holder for values of NSStrings
listed in the order you want after the comma.
Any other charactars will be included in the construction, in this case the new lines.
*/
[mutableString appendFormat:#"%#\n\n%#",shortGreetingString,longGreetingString];
NSLog (#"mutableString = %#" ,mutableString);
[pool drain];
I think this might help you. You'd rather to use '\r' instead of '\n'
I also had a similar problem and found \n works in LLDB but not in GDB
Try using NSString. You could use:
NSString *a = [NSString stringWithFormat:#"%#\n\n%#", #"Hi", #"Hello again"]
If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0
myView.numberOfLines=0;