nsstring

Find and replace long words in an NSString? [closed]

旧城冷巷雨未停 提交于 2019-11-28 07:05:38
问题 I'm trying to write a method that will search an NSString, determine if an individual word within the string is over 6 characters long and replace that word with some other word (something arbitrary like 'hello'). I am starting with a long paragraph and I need to end up with a single NSString object whose format and spacing has not been affected by the find and replace. 回答1: Why another answer? There are a couple of subtle problems with the simple solutions using componentsSeparatedByString:

Can't convert NSData to NSString in swift

╄→гoц情女王★ 提交于 2019-11-28 07:03:06
This is my function. First println print correct hash to console but in the next row program crashes. Can you help me? func sha256(string: NSString) -> NSString { var data : NSData! = string.dataUsingEncoding(NSUTF8StringEncoding) var hash = [UInt8](count: Int(CC_SHA256_DIGEST_LENGTH), repeatedValue: 0) CC_SHA256(data.bytes, CC_LONG(data.length), &hash) let res = NSData(bytes: hash, length: Int(CC_SHA256_DIGEST_LENGTH)) println(res) let resstr = NSString(data: res, encoding: NSUTF8StringEncoding) println(resstr) return resstr } let resstr = NSString(data: res, encoding: NSUTF8StringEncoding)

iOS - Most efficient way to find word occurrence count in a string

北城以北 提交于 2019-11-28 07:00:31
Given a string, I need to obtain a count of each word that appears in that string. To do so, I extracted the string into an array, by word, and searched that way, but I have the feeling that searching the string directly is more optimal. Below is the code that I originally wrote to solve the problem. I'm up for suggestions on better solutions though. NSMutableDictionary *sets = [[NSMutableDictionary alloc] init]; NSString *paragraph = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"text" ofType:@"txt"] encoding:NSUTF8StringEncoding error:NULL]; NSMutableArray

sizeWithFont doesn't give correct height for UITextView if there is a long string in the text being wrapped

血红的双手。 提交于 2019-11-28 06:56:44
Is there a way to get the correct size of an NSString using: - (CGSize)sizeWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)lineBreakMode that doesnt get thrown off by 2 or 3 hundred character strings. At the moment if I try to use this method on these long strings it incorrectly calculates them and I end up with lots of whitespace at the bottom of the UITextView. I've tried using UILineBreakModeWordWrap and UILineBreakModeCharacterWrap. the resizing is being done in - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSPredicate Exact Match with String

浪子不回头ぞ 提交于 2019-11-28 06:50:17
I have a NSPredicate like this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name CONTAINS %@", myString]; But that will return anything which contains that string. For example: If my entity.name's where: text texttwo textthree randomtext and the myString was text then all of those strings would match. I would like it so that if myString is text it would only return the first object with the name text and if myString was randomtext it would return the fourth object with the name randomtext . I am also looking for it to be case insensitive and that it ignores whitespace

Objective-C - Converting NSString to a C string [duplicate]

烂漫一生 提交于 2019-11-28 06:30:56
问题 This question already has an answer here : Closed 8 years ago . Possible Duplicate: objc warning: “discard qualifiers from pointer target type” I'm having a bit of trouble converting an NSString to a C string. const char *input_image = [[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]; const char *output_image = [[[NSBundle mainBundle] pathForResource:@"iphone_resized" ofType:@"png"] UTF8String]; const char *argv[] = { "convert", input_image, "-resize", "100x100",

Get the extension of a file contained in an NSString

夙愿已清 提交于 2019-11-28 06:09:14
I have an NSMutable dictionary that contains file IDs and their filename+extension in the simple form of fileone.doc or filetwo.pdf. I need to determine what type of file it is to correctly display a related icon in my UITableView. Here is what I have done so far. NSString *docInfo = [NSString stringWithFormat:@"%d", indexPath.row]; //Determine what cell we are formatting NSString *fileType = [contentFiles objectForKey:docInfo]; //Store the file name in a string I wrote two regex to determine what type of file I'm looking at, but they never return a positive result. I haven't used regex in iOS

Reliable way to compare two NSURL or one NSURL and an NSString?

梦想与她 提交于 2019-11-28 05:57:24
I recently had a problem when comparing two NSURLs and compare one NSURL with an NSString(which is a URL address), the situation is I got an NSURLRequest from somewhere, I may or may not know the URL address it points to, and I have an URL NSString, say "http://m.google.com", now I need to check if the URL in that NSURLRequest is the same as the URL string I had: [[request.URL.absoluteString lowercaseString] isEqualToString: [self.myAddress lowercaseString]]; this returns NO as the absoluteString gives me "http://m.google.com/" whereas my string is "http://m.google.com" without a slash in the

Replacing multiple characters in NSString by multiple other characters using a dictionary

房东的猫 提交于 2019-11-28 05:51:59
问题 I would like to replace some characters in my string, by other characters, using a dictionary. For instance, every "a" should be replaced by "1", and every "1" should be replaced by "9". What I don't want is every "a" to be replaced twice, ending up with a "9". Every character must be replaced just once. I got this working using the following code, but I feel like it can be done more efficient. Is this really the best I can do, or can you help me improve my code? NSDictionary *replacements =

Is there a way to use NSString stringByFoldingWithOptions to unfold the single French 'œ' character into 'oe'?

爱⌒轻易说出口 提交于 2019-11-28 05:10:47
问题 For a diacritics-agnostic full text search feature, I use the following code to convert accented characters like é or Ö into their lowercase non-accented form e and o [[inputString stringByFoldingWithOptions: NSCaseInsensitiveSearch + NSDiacriticInsensitiveSearch + NSWidthInsensitiveSearch locale: [NSLocale currentLocale]] lowercaseString]; This works. However, I found no way to convert special characters whose base form consists of multiple characters like the French œ (as in "sœur") or the