IOS find strings in the same substring location range

Source: Internet
Author: User

Problem: Resolve to replace multiple identical characters of the same string eg.

XXX This super big local tyrants white send xxx one! Hurry up and grab it!

Change the first xxx to a name

Replace the second XXX with an item

Two ways to be more flexible in the second approach

The first method is simple rough (train of thought to get the position of the first xxx appeared and then replaced with the name replaced after the string is only one xxx and then replaced with the item in the string only one xxx)

Nsrange range = [share6 rangeofstring:@ "xxx"];//get the first occurrence position

Share6 = [Share6 stringbyreplacingcharactersinrange:range withstring:_m_datadic[@ "nickname"];

Share5 = [Share5 stringbyreplacingcharactersinrange:range withstring:_m_datadic[@ "nickname"];

Sharecontent = [[_m_datadic[@] gender "]integervalue] = = 1?share5:share6 replace:@" xxx "withstring: ((!_m_datadic[@ "Content" | | [_m_datadic[@ "Content"] iskindofclass:[nsnull class]) [Email protected] "hitchhiking": _m_datadic[@ "Content"]);

The second method (the idea is to walk through the string first and then find all the xxx location of the index and then replace the string by index)

Nsmutablearray *arrayshare = [self getrangestr:share6 findtext:@ "xxx"];

Sharecontent = [[_m_datadic[@] gender "]integervalue] = = 1?share5:share6 Stringbyreplacingcharactersinrange: Nsmakerange ([Arrayshare[1]integervalue], 3) withstring: ((!_m_datadic[@ "content"] | | [_m_datadic[@ "Content"] iskindofclass:[nsnull class]) [Email protected] "hitchhiking": _m_datadic[@ "Content"]);

Sharecontent = [Sharecontent stringbyreplacingcharactersinrange:nsmakerange ([Arrayshare[0]integervalue], 3) withstring:_m_datadic[@ "nickname"];

Gets the index of all xxx in this string

-(Nsmutablearray *) Getrangestr: (NSString *) text findText: (NSString *) FindText

{

Nsmutablearray *arrayranges = [Nsmutablearray arraywithcapacity:20];

if (FindText = = Nil && [FindText isequaltostring:@ "]) {

return nil;

}

Nsrange rang = [text Rangeofstring:findtext]; Gets the range that appears for the first time

if (rang.location! = Nsnotfound && Rang.length! = 0) {

[Arrayranges Addobject:[nsnumber numberwithinteger:rang.location]];//is added to the array for the first time

Nsrange rang1 = {0,0};

Nsinteger location = 0;

Nsinteger length = 0;

for (int i = 0;; i++)

{

if (0 = = i) {//remove this XXX

Location = Rang.location + rang.length;

length = Text.length-rang.location-rang.length;

Rang1 = Nsmakerange (location, length);

}else

{

Location = Rang1.location + rang1.length;

length = Text.length-rang1.location-rang1.length;

Rang1 = Nsmakerange (location, length);

}

To find a range of another string within a range range

rang1 = [text Rangeofstring:findtext options:nscaseinsensitivesearch range:rang1];

if (rang1.location = = Nsnotfound && Rang1.length = = 0) {

Break

}else//Adding a qualified location-in array

[Arrayranges Addobject:[nsnumber numberWithInteger:rang1.location];

}

return arrayranges;

}

return nil;

}

IOS find strings in the same substring location range

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.