I am reading in 2-5 mb large txt files into nsstrings that I want to present in a uitextview.
Now I have experienced that it takes too long to read in the whole string especially since I am removing line breaks every time.
So I have decided separate the NSString into individual pages that I can navigate by two buttons ("previous" and "next"). The first thing I did was to decide to separate the NSString into 500 characters long substrings (then I remove line breaks before I present the string).
Now this works great and is fast enough, but there is one little problem that annoys me, the last word in the substring that is presented oftentimes gets cut off in the middle.
So what I did then was instead of using 500 characters to separate the substrings, to use 20 dots/periods ". ". This turned out to work very good also until I realized that not all txt files and texts that might be loaded will contain dots/periods because some languages might not have dots.
So I am looking for a solution where I can separate long text files into smaller substrings, about a page long, that I can navigate and that do not cut off the last word in the the substring in half. Any help would be appreciated.
Also I should add that I have tried to separate after x number words (i.e. x number white spaces) which I think might be the best solution but I cannot think of any other way but componentsSeparatedByString:#". " which takes to long because it goes through the whole string.
Is there some good way of enumerating a string but that still allows be to navigate through pages, perhaps by saving the substring range location or something?
Store all your data in a NSMutableArray, then enumerate by sentences in your string and add to a mutable array.
NSString * string = #"YOUR VERY LONG FILE";
NSMutableArray *array = [NSMutableArray arrayWithCapacity:100];
NSRange range = NSMakeRange(0, string.length);
[string enumerateSubstringsInRange:range options:NSStringEnumerationBySentences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
NSLog(#"substring: %#", substring);
NSLog(#"substringRange len:%lu, loc:%lu", substringRange.length, substringRange.location);
NSLog(#"enclosingRange len:%lu loc:%lu",enclosingRange.length, enclosingRange.location);
[array addObject:substring];
}];
Also take a look at other NSStringEnumeration options like ByLines, ByParagraphs or ByWords.
Related
I am facing one issue related some hexa value in string, i need to remove hexadecimal characters from NSString.
The problem is when i print object it prints as "BLANK line". And in debug mode it shows like :
So how can i remove it from the string?
EDIT
Triming whitespace :
result of NSLog is :
2015-12-14 15:37:10.710 MyApp [2731:82236] tmp :''
Database:
Earlier question:
how to detect garbage string value in ios?
As your dataset clearly has garbage values, You can use this method to check if your string is valid or not. Define your validation criteria and simply don't entertain the values which are garbage. But as suggested before by gnasher, you should rather look for the bug which is causing insertion of garbage data in your database. Once you have done that, check if the input string matches your defined criteria. If it does, do what you want. If it doesn't, simply move on.
-(BOOL) isValidString: (NSString*) input
{
NSMutableCharacterSet *validSpecialChars = [NSMutableCharacterSet characterSetWithCharactersInString:#"_~.,"];//Add your desired characters here
[validSpecialChars formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
return [[input stringByTrimmingCharactersInSet:validSpecialChars] isEqualToString:#""];
}
If your string will contain only your defined characters, it will return true. If it contains any other characters (garbage or invalid) it will return false.
I'm not sure exactly what you are looking for, but if you want to remove all the control characters then
string = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet controlCharacterSet]] componentsJoinedByString:#""]
If you need to be faster and are sure the control characters are only at the beginning and ending of a string then
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet controlCharacterSet]];
NOTE: Removing all control characters will remove all new lines (\n)!
From NSCharacterSet Class Reference:
These characters are specifically the Unicode values U+0000 to U+001F and U+007F to U+009F.
The value you are having a problem with is \x06 which is U+0006.
If you want to remove just \x06, then you can always create a characters set just for it.
NSCharacterSet *hex6 = [NSCharacterSet characterSetWithCharactersInString:#"\x06"];
string = [[string componentsSeparatedByCharactersInSet:hex6] componentsJoinedByString:#""]
First, don't trust the Xcode debugger. Print characterAtIndex:0 to be sure that you really have what you think you have.
Second, deleting stuff is all good and well, but you are doctoring around with a symptom. You should really try to figure out where the contents of _lastUpdatedBy comes from and why it is what it is. You might have a serious bug here and trying to cover it up. For example, there might be a bug that stores rubbish data instead of the correct data, and you are just covering up for that bug.
I have been having a lot of trouble with NSString's stringWithFormat: method as of late. I have written an object that allows you to align N lines of text (separated by new lines) either centered, right, or left. At the core of my logic is NSString's stringWithFormat. I use this function to pad my strings with spaces on the left or right of individual lines to produce the alignment I want. Here is an example:
NSString *str = #"$3.00" --> 3 dollars
[NSString stringWithFormat:#"%8s", [str cStringUsingEncoding:NSUnicodeStringEncoding]] --> returns --> " $3.00"
As you can see the above example works great, I padded 3 spaces on the left and the resulting text is right aligned/justified. Problems begin to arise when I start to pass in foreign currency symbols, the formatting just straight up does not work. It either adds extra symbols or just returns garbage.
NSString *str = #"Kč1.00" --> 3 Czech Koruna (Czech republic's currency)
[NSString stringWithFormat:#"%8s", [str cStringUsingEncoding:NSUnicodeStringEncoding]] --> returns --> " Kč1.00"
The above is just flat out wrong... Now I am not a string encoding expert but I do know NSString uses the international standardized unicode encoding for special characters well outside basic ASCII domain.
How can I fix my problem? What encoding should I use? I have tried so many different encoding enums I have lost count, everything from NSMACOSRomanEncoding to NSUTF32UnicodeBigEndian.. My last resort will be to just completely ditch using stringWithFormat all together, maybe it was only meant for simple UTF8Strings and basic symbols.
If you want to represent currency, is a lot better if you use a NSNumberFormatter with currency style (NSNumberFormatterCurrencyStyle). It reads the currentLocale and shows the currency based on it. You just need to ask its string representation and append to a string.
It will be a lot easier than managing unicode formats, check a tutorial here
This will give you the required result
NSString *str = #"Kč1.00";
str=[NSString stringWithFormat:#"%#%8#",[#" " stringByPaddingToLength:3 withString:#" " startingAtIndex:0],str];
Out Put : #" Kč1.00";
Just one more trick to achieve this -
If you like use it :)
[NSString stringWithFormat:#"%8s%#",[#"" cStringUsingEncoding:NSUTF8StringEncoding],str];
This will work too.
If I have a string in Objective-C called text that is a really long, such as:
NSString *text = [NSString stringWithFormat:#"%#", _sometext];
How can I scan _sometext and add a line break to the string every hundred characters (letters)?
So if I had the _sometext as
It was November. Although it was not yet late, the sky was dark when I
turned into Laundress Passage. Father had finished for the day,
switched off the shop lights and closed the shutters; but so I would
not come home to darkness he had left on the light over the stairs to
the flat.
How can I make it so it puts a line break after
It was November. Although it was not yet late, the sky was dark when I
turned into Laundress Passage
and after
. Father had finished for the day, switched off the shop lights and
closed the shutters; but so I wo
(since those were 100 characters)?
But instead of stopping in the middle of the word, I could it would skip the word and end at the previous word. Example: if the sentence ended at "but so I wo" and it cut of the word "would", it would stop at this instead of "but so I".
The easiest solution I can think of off the top of my head is a two step process.
Step one involves breaking the original string into an array of 100 character strings.
Step two involves joining that array of strings with the newline character.
NSMutableArray *lines = [NSMutableArray array];
while ([originalString length] > 100) {
[lines addObject:[originalString substringToIndex:100]];
originalString = [originalString substringFromIndex:100];
}
[lines addObject: originalString];
NSString *reformattedString = [lines componentsJoinedByString:#"\n"];
You could write code that would use the method rangeOfString:options:range:
to do that.
You'd create an NSRange for the first 100 characters of your string. Then you'd search backwards in that range for a space. When you found a space, you'd add from the beginning of the string to the space to your output string, plus a line break. Then you'd set your search range to the next 100 characters of your string after the space, and again search backwards for a space. Repeat until you've processed the entire string.
See the NSString class reference for the details on the rangeOfString:options:range: method.
i don't know whether this question has already been asked but i couldn't find anything about this.
I have a UITextView and i would like to be able to read single words from a text written by the users. This is an example:
i write on a textView this:
Obiwan is cool
then i have something like 3 string and store the single words that would result
string_1 = "Obiwan";
string_2 = "is";
string_3 = "cool";
What i'm looking for is like the Java's
s.useDelimiter(" ");
plus, to read the tableView,
while(s.hasNext()){
...
}
Thanks in advance.
You need to use NSString's equivalent of Java's split(), which is either
componentsSeparatedByCharactersInSet:
or
componentsSeparatedByString:
Both of these return an NSArray of word strings. If you pass a single space to the latter, it will do exactly what your Java example does. However, if you want to be more precise by including other whitespace and newlines, you use the former with an NSCharacterSet, like
NSArray *words = [self.textView.text componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
So as I work my way through understanding string methods, I came across this useful class
NSCharacterSet
which is defined in this post quite well as being similar to a string excpet it is used for holding the char in an unordered set
What is differnce between NSString and NSCharacterset?
So then I came across the useful method invertedSet, and it bacame a little less clear what was happening exactly. Also I a read page a fter page on it, they all sort of glossed over the basics of what was happening and jumped into advanced explainations. So if you wanted to know what this is and why we use It SIMPLY put, it was not so easy instead you get statements like this from the apple documentation: "A character set containing only characters that don’t exist in the receiver." - and how do I use this exactly???
So here is what i understand to be the use. PLEASE provide in simple terms if I have explained this incorrectly.
Example Use:
Create a list of Characters in a NSCharacterSetyou want to limit a string to contain.
NSString *validNumberChars = #"0123456789"; //Only these are valid.
//Now assign to a NSCharacter object to use for searching and comparing later
validCharSet = [NSCharacterSet characterSetWithCharactersInString:validNumberChars ];
//Now create an inverteds set OF the validCharSet.
NSCharacterSet *invertedValidCharSet = [validCharSet invertedSet];
//Now scrub your input string of bad character, those characters not in the validCharSet
NSString *scrubbedString = [inputString stringByTrimmingCharactersInSet:invertedValidCharSet];
//By passing in the inverted invertedValidCharSet as the characters to trim out, then you are left with only characters that are in the original set. captured here in scrubbedString.
So is this how to use this feature properly, or did I miss anything?
Thanks
Steve
A character set is a just that - a set of characters. When you invert a character set you get a new set that has every character except those from the original set.
In your example you start with a character set containing the 10 standard digits. When you invert the set you get a set that has every character except the 10 digits.
validCharSet = [NSCharacterSet characterSetWithCharactersInString:validNumberChars];
This creates a character set containing the 10 characters 0, 1, ..., 9.
invertedValidCharSet = [validCharSet invertedSet];
This creates the inverted character set, i.e. the set of all Unicode characters without
the 10 characters from above.
scrubbedString = [inputString stringByTrimmingCharactersInSet:invertedValidCharSet];
This removes from the start and end of inputString all characters that are in
the invertedValidCharSet. For example, if
inputString = #"abc123d€f567ghj😄"
then
scrubbedString = #"123d€f567"
Is does not, as you perhaps expect, remove all characters from the given set.
One way to achieve that is (copied from NSString - replacing characters from NSCharacterSet):
scrubbedString = [[inputString componentsSeparatedByCharactersInSet:invertedValidCharSet] componentsJoinedByString:#""]
This is probably not the most effective method, but as your question was about understanding
NSCharacterSet I hope that it helps.