An NSString that requires some investigation
I have a text file that I need to analyze. The file contains a series of short sentences each on a new line. I am doing this by building an array of lines, using componentsSeparatedByString:@"\n"
This works fine for the most part. If I log @"This is the string ->%@<-T开发者_JAVA百科hat was the string" I get
This is the string ->line1<-That was the string
This is the string ->line2<-That was the string
This is the string ->line3<-That was the string
But for some lines, I see a carriage return:
This is the string ->line1<-That was the string
This is the string ->line2
<-That was the string
This is the string ->line3<-That was the string
I don't see anything different about the line in the file, so tried copying and pasting it into word. I see that for the troublesome section, the lines are double spaced in word. The other sections, where I don't have a problem are not.
What is going on here? Are there some hidden characters getting picked up as part of the NSString? How do I start to investigate this?
Look at your file with hexdump
and see if you have mixed line endings. You might have some lines that end with \n
and others that end with \r\n
, and that could be confusing things. In general, making sure that your input is clean is always a good idea! Barring that, you'll need to extend your program to take care of whatever case is messing it up. Poking around at your strings in the debugger will point you in the right direction, too.
精彩评论