0

in objective c i want to replace string which is between two string.

For example "ab anystring yz"

i want to replace string which is between "ab" and "yz".

is it possible to do? Please Help

thx in advance.

3 Answers 3

4

Here is tested code to perform your task.

NSString *string = @"ab anystring yz";
NSString *result = nil;
// Determine "ab" location
NSRange divRange = [string rangeOfString:@"ab" options:NSCaseInsensitiveSearch];
if (divRange.location != NSNotFound)
{
    // Determine "ab" location according to "yz" location
    NSRange endDivRange;

    endDivRange.location = divRange.length + divRange.location;
    endDivRange.length   = [string length] - endDivRange.location;
    endDivRange = [string rangeOfString:@"yz" options:NSCaseInsensitiveSearch range:endDivRange];

    if (endDivRange.location != NSNotFound)
    {
        // Tags found: retrieve string between them
        divRange.location += divRange.length;
        divRange.length = endDivRange.location - divRange.location;

        result = [string substringWithRange:divRange];

        string =[string stringByReplacingCharactersInRange:divRange withString:@"Replace string"];
    }
}

Now you just check the string you will get ab Replace string yz

Sign up to request clarification or add additional context in comments.

Comments

2
NSString *newString = [(NSString *)yourOldString stringByReplacingOccurrencesOfString:@"anystring" withString:@""];

otherwise you're going to have to get a copy of REGEXKitLite and use a regex function like:

NSString *newString = [(NSString *)yourOldString stringByReplacingOccurrencesOfRegex:@"ab\\b.\\byz" withString:@"ab yz"];

5 Comments

Thanks Thomas.I tried first one but it doesn't work.I am not clear about second answer.In my case string between "ab" and "yz" is not fixed length string.help me if is it possible.
afaik a fullstop . in regex means "any amount of characters". You need to download regexKitLite and include it in your project.
Hi Thomas Thanks again. I tried it.No compilation error but might be some logic error.I put the code.Tell me if u have any suggestion for it. NSString *str = [[NSString alloc] init]; str = @"ab This is demo yz"; NSString *newstr = [[NSString alloc] init]; newstr = [str stringByReplacingOccurrencesOfRegex:@"ab\\b.\\byz" withString:@"good"];
I got the answer. NSString str = [[NSString alloc] init]; str = @"ab this is demo yz"; NSString *newstr = [[NSString alloc] init]; newstr = [str stringByReplacingOccurrencesOfRegex:@" (.) " withString:@"good"];
May I suggest that instead of doing NSString *str = [[NSString alloc] init]; str= @"Hello"; You can just do: NSString *str = [NSString stringWithFormat:@"Hello"]; then you don't have to worry about memory management or anything. :) and its just a little nicer to code and read. :)
-1
    -(NSString*)StrReplace :(NSString*)mainString preMatch:(NSString*) preMatch postMatch:(NSString*) postMatch replacementString:(NSString*) replacementString
    {
        @try

        {

            if (![mainString isEqualToString:@""] || [mainString isKindOfClass:[NSNull class]] || mainString==nil)
            {


                NSArray *preSubstring = [mainString componentsSeparatedByString:preMatch];
                NSString *preStr = [preSubstring objectAtIndex:0];

                NSArray *postSubstring = [mainString componentsSeparatedByString:postMatch];
                NSString *postStr = [postSubstring objectAtIndex:1];

                NSString *resultStr = [NSString stringWithFormat:@"%@%@%@%@%@" ,preStr,preMatch,replacementString,postMatch,postStr];

                return resultStr;

            }
            else
            {
                return @"";
            }

        }
        @catch (NSException *exception) {
        }
    }

1 Comment

Please edit your question: (i) add an explanation to your code (just code is not an answer to an 'is it possible' question) and (ii) fix your code block (since not all code is in the code block).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.