IOS gets all substrings rang

Source: Internet
Author: User

App development now requires more and richer effects, and a variety of special effects emerge. Animated effects and text are mostly displayed in rich text.

On the internet also saw several methods, feel some of the trouble of dealing with, below is I summarize to get substring of all rang some methods:

string to do rich text processing need to pass nsmutableattributedstring to process, create a nsmutableattributedstring object First:

nsmutableattributedstring *attribtstr = [[Nsmutableattributedstring alloc] initwithstring:@ "parent string"];

1, using nsregularexpression to handle

Initialize Nsregularexpression
Nsregularexpression *regularexpression = [nsregularexpression regularexpressionwithpattern:@"your regular expression"Options0error:nil];//iterates through a string, returns the state of a substring in usingblock, and processes substrings in Usingblock
[RegularExpression enumeratematchesinstring:attribtstr.stringOptions0Range:nsmakerange (0, Attribtstr.string. Length) usingblock:^ (Nstextcheckingresult * _nullable result, nsmatchingflags flags, BOOL *_nonnull stop) {//Add double strikethrough to substring [attribtstr addattribute:nsstrikethroughstyleattributename value:[nsnumber Numberwithinteger : Nsunderlinestyledouble] range:result.range]; Strikethrough set to red [Attribtstr addattribute:nsstrikethroughcolorattributename value:[uicolor Redcolor] range:result.range]; }];

The above method is to work directly in the traversal of the matching substring, the following is the first to get all the substring of the state set, and then do the corresponding processing:

Nsarray *regularexpressionarr = [RegularExpression matchesinstring:attribtstr.  String options:0 Range:nsmakerange (0, attribtstr.  String. Length)];

Gets the array of substrings, what needs to be done, and can be completed by loop judgment.

 for inch Regularexpressionarr) {        = [Result range];        NSLog (@ "%lu", rang.location);                [Attribtstr addattribute:nsstrikethroughstyleattributename value:[nsnumber Numberwithinteger: Nsunderlinestyledouble] Range:result.range];        [Attribtstr addattribute:nsstrikethroughcolorattributename Value:[uicolor Redcolor] range:result.range];    }

The following methods of Nsregularexpression are also useful in other situations:

Gets the number of substrings
-(Nsuinteger) numberofmatchesinstring: (NSString *)string options: (nsmatchingoptions) options Range: ( Nsrange) range;//Gets the nstextcheckingresult of the first substring
-(Nullable Nstextcheckingresult *) firstmatchinstring: (NSString *)string options: (nsmatchingoptions) Options Range: (Nsrange) range;//gets the first qualifying substring within a specified rang
-(Nsrange) rangeoffirstmatchinstring: (NSString *)string options: (nsmatchingoptions) options range: (Nsrange ) range;

2. Rang array of all substrings by recursion (the pros and cons of recursion are not pulled, use caution)

//get all substring location by recursion- (void) Rangeofstring: (nsstring*) searchstring fatherstring: (nsstring*) fatherstr options: (nsstringcompareoptions) Mask range: (nsrange) Rangeofreceivertosearch {//Gets the first matched substring in the specified range rang,  the same way as the one above nsregularexpression.
Nsrange rang=[Fatherstr rangeofstring:searchstring options:mask Range:rangeofreceivertosearch];
Determine the search scope to determine whether to complete the searchif(Rang.location >fatherstr.length-searchstring.length) {return; }
Nsrang cannot be stored in an array, so here is the location of rang [StrLocationrangarr addobject:[nsnumber Numberwithinteger: Rang.location]];
Recursive search [self rangeofstring:searchstring fatherstring:fatherstr options:mask range:nsmakerange (rang.location+searchstring.length, fatherstr.length-rang.location-searchstring.length)];}

Invocation Example:

StrLocationRangarr =[Nsmutablearray array]; [Self rangeofstring:@"Coretext"Fatherstring:attribtstr.stringOptions0Range:nsmakerange (0, Attribtstr.string. length)];  for(NSNumber *locationinchStrLOcationrangarr) {[Attribtstr addattribute:nsstrikethroughstyleattributename value:[nsnumber Numberwithinteger:nsunderlinestyledou BLE] Range:nsmakerange ([location IntegerValue],8)]; [Attribtstr addattribute:nsstrikethroughcolorattributename Value:[uicolor Redcolor] Range:NSMakeRange ([location IntegerValue],8)]; }

3. Via string -(nsarray<nsstring *> *) componentsseparatedbystring: (NSString *) separator; The Rang method obtains a substring of the array to be processed by the string collection.

Nsarray *arr = [Attribtstr.  String componentsseparatedbystring:@ "searchstring"];

Matches the ATTRIBTSTR by the substring of the array. string Gets the location of the searchstring ,

This method needs to deal with a lot of places, first of all to consider whether the first, last and continuous in arr is @ "", in the array is inevitably the same cut to the same substring.

For example: @ "123654789123654789" so to [email protected] "654" will appear two @ "123" and @ "789", at the time of judgment to deal with the repetition problem here, Small indicates that there are two ways to do this without further in-depth implementation of the idea.

relatively nsregularexpression simple and fast, other methods used as a way of thinking is still good.

IOS gets all substrings rang

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.