开发者

How a get a part of the string from main String in Objective C

开发者 https://www.devze.com 2022-12-17 00:19 出处:网络
I have mainStr开发者_运维技巧ing from which i need to get the part of the string after finding a keyword.

I have mainStr开发者_运维技巧ing from which i need to get the part of the string after finding a keyword.

NSString *mainString = "Hi how are you GET=dsjghdsghghdsjkghdjkhsg";

now I need to get the string after the keyword "GET=".

Waiting for a reply.


Have a look at the NSString documentation.

Assuming your string really is so totally straightforward, you could do something like this:

NSArray *components = [mainString componentsSeparatedByString: @"GET="];
NSString *stringYouWant = [components objectAtIndex: 1];

Obviously, this performs absolutely no error checking and makes a number of assumptions about the actual contents of mainString, but it should get you started.

Note, also, that the code is somewhat defensive in that it assumes that you are looking for GET= and not separating on =. Either way is a hack in terms of parsing, but... hey... hacks are sometimes the right answer.


You can use a regex via RegexKitLite:

NSString *mainString = @"Hi how are you GET=dsjghdsghghdsjkghdjkhsg";
NSString *matchedString = [mainString stringByMatching:@"GET=(.*)" capture:1L];
// matchedString == @"dsjghdsghghdsjkghdjkhsg";

The regex used, GET=(.*), basically says "Look for GET=, and then grab everything after that". The () specifies a capture group, which are useful for extracting just part of a match. Capture groups begin at 1, with capture group 0 being "the entire match". The part inside the capture group, .*, says "Match any character (the .) zero or more times (the *)".

If the string, in this case mainString, is not matched by the regex, then matchedString will be NULL.


You can get the location of the first occurrence of = and then just take a substring of mainString from the location of = to the end of the string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号