nsstring

How to get the first N words from a NSString in Objective-C?

我与影子孤独终老i 提交于 2019-11-28 05:08:27
What's the simplest way, given a string: NSString *str = @"Some really really long string is here and I just want the first 10 words, for example"; to result in an NSString with the first N (e.g., 10) words? EDIT: I'd also like to make sure it doesn't fail if the str is shorter than N. Barry Wark If the words are space-separated: NSInteger nWords = 10; NSRange wordRange = NSMakeRange(0, nWords); NSArray *firstWords = [[str componentsSeparatedByString:@" "] subarrayWithRange:wordRange]; if you want to break on all whitespace: NSCharacterSet *delimiterCharacterSet = [NSCharacterSet

How can I extract a URL from a sentence that is in a NSString?

冷暖自知 提交于 2019-11-28 04:33:12
What I'm trying to accomplish is as follows. I have a NSString with a sentence that has a URL within the sentience. I'm needing to be able to grab the URL that is presented within any sentence that is within a NSString so for example: Let's say I had this NSString NSString *someString = @"This is a sample of a http://abc.com/efg.php?EFAei687e3EsA sentence with a URL within it."; I need to be able to extract http://abc.com/efg.php?EFAei687e3EsA from within that NSString. This NSString isn't static and will be changing structure and the url will not necessarily be in the same spot of the

How can I use the \t [tab] operator to format a NSString in columns?

妖精的绣舞 提交于 2019-11-28 04:18:24
问题 I would like to find a way to format a NSString like this: Name: John Location: Unknown Type: Unknown Status: Unknown Right now I'm using this code to do it but the result is quite different: The code: serializedValue = [serializedValue stringByAppendingFormat:@"%@:\t\t%@\n",title, value]; Where title is the left column and value is the second one. The result: Name: John Location: Unknown Type: Unknown Status: Unknown Now, as you can see, adding always a constant number of tabs (\t), 2 in my

NSString immutable allows to change its values?

落花浮王杯 提交于 2019-11-28 04:12:54
问题 The code below compiles and runs, BUT according to all iPhone development books and Apple documentation it shouldnt! Can someone please explain to me how come immutable NSString allows to change its values after it has been set? I thought I had to use NSMuttableString to change context of the same string variable? I am using SDK 3.1. NSString *test =[[NSString alloc] initWithString:@"TEST"]; [test release]; test = @"TEST2"; 回答1: Perhaps the following example will add upon the replies from

How to get iOS appStoreReceiptURL into Base 64 Encoded String?

柔情痞子 提交于 2019-11-28 04:12:18
I want to use appStoreReceiptURL to see which version of the app someone purchased. How can I get this into a string? I'm testing this by downloading the app from the store, then running a new version of the app from Xcode. Here is what I've tried: NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL]; NSLog(@"receiptUrl %@",[receiptUrl path]); if ([[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) { NSLog(@"exists"); NSError *error; NSString *receiptString = [[NSString alloc] initWithContentsOfFile:[receiptUrl path] encoding:NSUTF8StringEncoding error:&error]; if

How to quickly check if NSString object is a valid url?

安稳与你 提交于 2019-11-28 03:40:09
问题 Help me to write the code like "if my string is a valid URL do smth" Is it possible to write this in a couple strings of code? 回答1: I will assume that by URL, you are referring to a string identifying a internet resource location. If you have an idea about the format of the input string , then why not manually check if the string starts with http:// , https:// or any other scheme you need. If you expect other protocols, you can also add them to the check list (e.g. ftp:// , mailto:// , etc)

How to convert std::string to NSString?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 03:37:50
Hi I am trying to convert a standard std::string into an NSString but I'm not having much luck. I can convert successfully from an NSString to a std::string with the following code NSString *realm = @"Hollywood"; std::string REALM = [realm cStringUsingEncoding:[NSString defaultCStringEncoding]]; However I get a compile time error when I try the following NSString *errorMessage = [NSString stringWithCString:REALM encoding:[NSString defaultCStringEncoding]]; The error I get is Cannot convert 'std::string' to 'const char*' in argument passing Am I missing something here? Thanks in advance. Get c

Finding out whether a string is numeric or not

你说的曾经没有我的故事 提交于 2019-11-28 03:31:36
How can we check if a string is made up of numbers only. I am taking out a substring from a string and want to check if it is a numeric substring or not. NSString *newString = [myString substringWithRange:NSMakeRange(2,3)]; Here's one way that doesn't rely on the limited precision of attempting to parse the string as a number: NSCharacterSet* notDigits = [[NSCharacterSet decimalDigitCharacterSet] invertedSet]; if ([newString rangeOfCharacterFromSet:notDigits].location == NSNotFound) { // newString consists only of the digits 0 through 9 } See +[NSCharacterSet decimalDigitCharacterSet] and -

Why are these two NSString pointers the same?

守給你的承諾、 提交于 2019-11-28 03:31:20
问题 When I alloc and init two NSString variables and compare their pointers, they are the same. Here's a snippet that shows this: NSString *s1 = [[NSString alloc] initWithString:@"hello world"]; NSString *s2 = [[NSString alloc] initWithString:@"hello world"]; if (s1 == s2) { NSLog(@"=="); }else { NSLog(@"!="); } Why are s1 and s2 the same? 回答1: There are three things going on here: Firstly, the two identical string literals you're passing in to initWithString: will have the same address to start.

NSString copy not copying?

…衆ロ難τιáo~ 提交于 2019-11-28 03:25:16
问题 NSString *myString = @"sample string"; NSString *newString = [myString copy]; If I set a breakpoint after these two lines, the pointer for myString is the same as the pointer for newString. WTF? Isn't NSString copy supposed to return a pointer to a new object? Or am I missing something fundamental about how copy is supposed to work? 回答1: Since NSString is not mutable it might just internally increase ref count and be done with it. When you release one of those NSStrings it might just