Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C: Best way to extract substring from NSString?

I have a space-separated string like @"abc xyz http://www.example.com aaa bbb ccc".

How can I extract the substring @"http://www.example.com" from it?

like image 223
ThyPhuong Avatar asked Oct 23 '09 09:10

ThyPhuong


People also ask

How do I get substring in Objective-C?

NSString *needle = [haystack componentsSeparatedByString:@":"][1]; This one creates three temporary strings and an array while splitting. All snippets assume that what's searched for is actually contained in the string. Ky.

What is OBJC?

Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.

What is NSString?

A static, plain-text Unicode string object that bridges to String ; use NSString when you need reference semantics or other Foundation-specific behavior.

How do I append to NSString?

Working with NSString. Instances of the class NSString are immutable – their contents cannot be changed. Once a string has been initialized using NSString, the only way to append text to the string is to create a new NSString object. While doing so, you can append string constants, NSString objects, and other values.


1 Answers

I know this is a very late reply, but you can get the substring "http://www.abc.com" with the following code:

[@"abc xyz http://www.abc.com aaa bbb ccc" substringWithRange:NSMakeRange(8, 18)] 

Of course, you can still use an NSString for that.

like image 110
Justin Avatar answered Sep 26 '22 01:09

Justin